How can a socket be both connected and closed?

前端 未结 1 497
[愿得一人]
[愿得一人] 2020-12-03 04:48

I\'m using a Java socket client. In a case where the server is still connected to my client but it does not send a response to my message - I eventually get a read time out

相关标签:
1条回答
  • 2020-12-03 05:26

    This thread has some useful discussions on this topic. It turns out that Socket.isConnected returns true if it has (ever) been successfully connected.

    From the above thread:

    When you use Socket(), which you seem to have overlooked, Socket.isConnected() tells you whether Socket.connect() has been called or not. Similarly for isClosed() and close().

    Confusion over these methods results from confusing the state of the socket, which is under the control of the application, with the state of the overall connection, which is under the control of the protocol. isConnected() and isClosed() tell what you have done to the socket. There are no APIs other than read and write for determining the state of the connection.

    The docs says:

    Returns true if the socket successfuly connected to a server

    and not as one perhaps would expect "returns true if the socket is connected to a server".

    The behavior can be confirmed by looking at the source of Socket:

    public boolean isConnected() {
        // Before 1.3 Sockets were always connected during creation
        return connected || oldImpl;
    }
    

    You could also run this little test snippet:

    Socket s = new Socket();
    
    System.out.println("isConnected: " + s.isConnected() +
                      " isBound: "     + s.isBound() +
                      " isClosed: "    + s.isClosed());
    
    s.connect(new InetSocketAddress("google.com", 80));
    
    System.out.println("isConnected: " + s.isConnected() +
                       " isBound: "    + s.isBound() +
                       " isClosed: "   + s.isClosed());
    
    s.close();
    
    System.out.println("isConnected: " + s.isConnected() +
                       " isBound: "    + s.isBound() +
                       " isClosed: "   + s.isClosed());
    

    Which prints:

    isConnected: false isBound: false isClosed: false
    isConnected: true isBound: true isClosed: false
    isConnected: true isBound: true isClosed: true
    

    I must say that the documentation is quite unclear on this point, and that the method-name is a bit misleading.

    0 讨论(0)
提交回复
热议问题