traveh
traveh

Reputation: 2884

Socket not connecting after first connection failure

I don't understand why the following program doesn't work when the initial connect fails (I intentionally start the server socket after the first connect fails):

Socket client = new Socket();
while (true) {
    try {
        client.connect(address);
        break;
    } catch (IOException e) {
         Thread.sleep(1000);
    }
}

If I move the client = new Socket(); inside the while loop, it works, but I couldn't find anything in the documentation that states that if an initial connect fails the socket is "broken" and has to be recycled.

Upvotes: 1

Views: 836

Answers (1)

user207421
user207421

Reputation: 310903

You can't reconnect a socket that has already failed to connect. You have to close it and try again with a new socket. The documentation you're looking for would be at the Berkeley Sockets API level, not Java,

Upvotes: 2

Related Questions