Skip to content
Advertisement

If a peer client disconnects from a server could we just ignore the socketexception at the server side?

I have a server which accepts input from client; it reads text input from client and processes it in some way(each new connection is assigned a thread also). However, whenever a client suddenly disconnects from the server I receive a socketException(connection reset) from the server where BufferedReader.readLine() is invoked. So basically this exception is an indicator that a client was suddenly terminated.

So in this case could I just ignore the exception occurring at the server and simply just close that socket? I’m just wondering because it’s not very usual in java to just “ignore” exceptions, however I haven’t found any other solutions other than just to take no notice of the exception.

error:

java.net.SocketException: Connection reset

Error occurs whenever we read data from inputstream:

    @Override
public void run() {

    

    try (
            Socket sock = socket; 
            PrintWriter out = new PrintWriter(sock.getOutputStream());
            BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
            ){

        String receivedData;
        
        while((receivedData = in.readLine()) != null){ 
                
                out.println(receivedData);
                out.flush();
        }
        
    } catch (IOException e) {
        e.printStackTrace();
        System.out.println("Client disconnected: " + socket);
        
    } 
} 

Advertisement

Answer

I read your other question, about the exception on close connection, and in both of your questions you are asking if it’s a good idea to ignore them: The answer is NOT. The second question is How I deal with them.

Well, you need to understand why these exceptions are triggered, and for that, you need to understand the TCP connection, I shared to you some links:

https://support.huawei.com/enterprise/mx/doc/EDOC1100058931/a1faac62/tcp

java.net.SocketException: Socket closed means that some of the pears closing the connection but your program is in a process of reading or writing…

        while((receivedData = in.readLine()) != null){ 
                
                out.println(receivedData);
                out.flush();
        }

…these operations are interrupted at the moment of closing the socket. For that reason, these functions, readline or printline, trigger an exception that should be handled to close the socket that triggered the exception to avoid leaving connections on CLOSE_WAIT, as you can see in this link, which could be considered as a performance issue into your program:

https://www.ibm.com/docs/en/zos/2.1.0?topic=SSLTBW_2.1.0/com.ibm.zos.v2r1.halu101/constatus.htm

Ok, the same thing happens when the connection has lost unexpectedly (java.net.SocketException: Connection reset.). In that case, the exception means that The connection was unexpectedly closed by the peer without starting a close process. In that case, the remote point has gone and your socket should close to release all the resources that this socket uses.

What happened if you don’t handle those exceptions: The socket will be unavailable to be reused until the SO release them, which could take several minutes and run out the available sockets in cases of high stress.

For those reasons and many more, it’s important to take care of socket exceptions.

Advertisement