I made a socket client pool from which I get a Socket con to send message to a socket server.Then when the socket server is closed,how can I test whether the connection is ok? I have tried con.getRemoteSocketAddress(); con.isConnected(); con.isClosed(); they are ok,but in fact the socket server has been closed.How can I do?
Enabling SO_KEEPALIVE will detect a broken connection, but only after a while, and at a cost. The only surefire way is to introduce a no-op test message into your application (call it "ping"), send that message to the server, and see if you get an exception thrown in your face. And even then there's a possibility that the connection will go down between pinging it and returning it to the caller -- so the calling code will have to be pepared to recover from network problems whatever you do. - Peter