Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why a "java.net.SocketException: Broken pipe" will occur?

Tags:

java

sockets

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 "java.net.SocketException: 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());
    out.flush();

}

Detail Error from console:

java.net.SocketException: Broken pipe
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
    at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
    at java.io.ObjectOutputStream$BlockDataOutputStream.drain(ObjectOutputStream.java:1847)
    at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(ObjectOutputStream.java:1756)
    at java.io.ObjectOutputStream.<init>(ObjectOutputStream.java:230)
like image 298
DNB5brims Avatar asked Mar 02 '12 04:03

DNB5brims


2 Answers

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).

like image 158
Jim Garrison Avatar answered Sep 25 '22 12:09

Jim Garrison


I think this happens when remote end closed the connection;

like image 41
Santhosh Kumar Tekuri Avatar answered Sep 22 '22 12:09

Santhosh Kumar Tekuri