view raw
DNB5brims DNB5brims - 6 months ago 51
Java Question

Why a " Broken pipe" will occur?

I wrote a simple socket programme, it works fine, but my friend use a port scanning tool, when it scan to the port I am using, it cash with " Broken pipe" error, what's happen? and how to solve it?

providerSocket = new ServerSocket(portNum);
connection = providerSocket.accept();
if (connection.getOutputStream() != null) {
//this line crash!!!
out = new ObjectOutputStream(connection.getOutputStream());


Detail Error from console: Broken pipe
at Method)


Some port scanners work by starting to open a connection and then immediately terminating it. Your server is not programmed to deal with a connection failure because you did not code for that possibility. You will need to use a try/catch to trap that condition and recover. Also, you should probably be handing off the connection to a separate thread for processing, so your main program can continue to receive new connections (and sending them to threads to be handled).