Reputation: 11
The following bit of code throws java.net.SocketTimeoutException: Accept timed out
:
ServerSocket serverSocket = new ServerSocket(0, 1, InetAddress.getLocalHost());
serverSocket.setSoTimeout(6000);
serverSocket.accept();
I have tried changing everything I can in creating a ServerSocket
but the error remains the same. Please guide me in what I'm missing here, if anything.
Upvotes: 1
Views: 24856
Reputation: 719551
What your code is doing is listening for 6 seconds for incoming TCP/IP requests on port zero for the local host1.
Here are some reasons why you might get a SocketTimeoutException
.
1 - If you don't want that "only accept an exception if it arrives within 6 seconds" behaviour ... which strikes me as a bit odd ... you shouldn't set a timeout on the server socket object.
Upvotes: 4