• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem of Socket Pool  RSS feed

 
alex shaw
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!