how to check active connections in both client and server applications
Posted on 2004-11-22
I have been using a free example client server program "echo server" , I wanted to learn how to test the following conditions so I could apply them to my application.
I want my client application to continuously attempt to connect to the server until a connection is made,
if the server is not running yet, I want the client to continue to try until it is.
If the server dies while the client is connected, I want the client to go back into a continuous
reconnect loop until another connection is made.
I've done the first part as desired, by making a function return the Socket object and continuously
making the function call while(socket != null)
I have not been able to get the second condition of going into a reconnect state if the server dies.
I thought since I have the socket object reference I could use one of the methods provided
by the Socket class to test for an active connection and if not make the reconnect function call
My problem is, I start the client and it trys to connect to the server continuously and then I start the server
and all is good. I kill the server and make all of those function calls in the client looking for one of them to give me some indication that the server is dead, but all of them return inaccurate results.
mySocket.isInputShutdown( ) returns false
mySocket.isOutputShutdown( ) returns false
mySocket.isConnected( ) returns true
mySocket.isBound( ) returns true
mySocket.isClosed( ) returns false
I had the client make these function calls long after I stopped the server, because I thought maybe
there was a timeout that wasn't timed out to release resources, I checked the sockets time out
settings and there is no time out set
Finally my question, how can a client application test whether or not it is still connected to a server?
why don't any of those function calls detect that the server has died?