Checking if a ClientSocket has disconnected in java hangs

后端 未结 3 1926
野的像风
野的像风 2020-12-10 08:59

This is a follow up to:

this question

Basically, I have a server loop that manages a connection to one solitary client. At one point in the loop, if a Clien

3条回答
  •  孤街浪徒
    2020-12-10 09:15

    When you create your socket, first set a timeout:

    private int timeout    = 10000;
    private int maxTimeout = 25000;
    
    clientSocket.setSoTimeout(timeout);
    

    With this, if a read times out you'll get java.net.SocketTimeoutException (which you have to catch). Thus, you could do something like this, assuming you've previously set the SO_TIMEOUT as shown above, and assuming that the heartbeat will always get a response from the remote system:

    volatile long lastReadTime;
    
    try {
        bufferedReader.read();
        lastReadTime = System.currentTimeMillis();
    } catch (SocketTimeoutException e) {
        if (!isConnectionAlive()) {
            logger.info("CONNECTION TERMINATED!");
            clientSocket.close(); 
            setUpSocket(); //sets up the server to reconnect to the client
        } else {
            sendHeartBeat(); //Send a heartbeat to the client
        }
    }
    
    public boolean isConnectionAlive() {
        return System.currentTimeMillis() - lastReadTime < maxTimeout;
    }
    

    A common way of handling this is setting the timeout to some number (say 10 seconds) and then keeping track of the last time you successfully read from the socket. If 2.5 times your timeout have elapsed, then give up on the client and close the socket (thus sending a FIN packet to the other side, just in case).

    If the heartbeat will not get any response from the remote system, but is just a way of ultimately generating an IOException earlier when the connection has fallen down, then you could do this (assuming that the sendHeartBeat itself will not throw an IOException):

    try {
        if (bufferedReader.read() == -1) {
            logger.info("CONNECTION TERMINATED with EOF!");
            resetConnection();
        }
    } catch (SocketTimeoutException e) {
        // This just means our read timed out ... the socket is still good
        sendHeartBeat(); //Send a heartbeat to the client
    } catch (IOException e) {
        logger.info("CONNECTION TERMINATED with Exception " + e.getMessage());
        resetConnection();
    }
    
    ....
    
    private void resetConnection() {
        clientSocket.close(); 
        setUpSocket(); //sets up the server to reconnect to the client
    }
    

提交回复
热议问题