in my application I'm waiting for a server to be available in a loop:
Socket sock=new Socket(); // create the socket object
while (running) // a more or less endless loop
{
try
{
sock.connect(new InetSocketAddress(data.host,11355),1000); // try to connect to the server
...
// when everything goes well, we do something with the "sock"
}
catch (IOException ioe)
{
// conection could not be established
try
{
Thread.sleep(1000); // wait for one second until next connection attempt
}
catch (InterruptedException ie)
{
}
}
}
The idea is to try to establish a connection to a server, when it fails an exception is thrown by connect() and I try again after a delay of one second.
My problem: it does not work. When the server is not available at application start-up but becomes accessible later, connect() is not successful, it can't connect to the server. This code works well only in case the server is available for the first connect().
So what could be wrong here? Do I have to reset the Socket somehow? Or what else could be the reason later calls to connect() never are successful?
Thanks!