Home > database >  If a peer client disconnects from a server could we just ignore the socketexception at the server si
If a peer client disconnects from a server could we just ignore the socketexception at the server si

Time:10-25

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);
        
    } 
} 

CodePudding user response:

It's not a good idea to ignore this type of error. But if you really want to do it, you can do something like :

try (
   Socket socket = this.socket;
   socket.doSomething(); // here do what you want
} catch (SocketException e) { // your error
   System.out.println("Connection failed."); // jus tlog it but without throwing error
} catch (IOException e) { // all others errors
   e.printStackTrace();
   System.out.println("Client disconnected: "   socket);
} 

The best way is to do something specific for specific error. Such as SocketException is an important error when socket is a very important way in your app, you should try to get them back. If you failed (user disconnect to internet ...) you should tell it to current user with alert/popup or log.

CodePudding user response:

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.

  • Related