2

What does an IOException indicate when it is thrown from ServerSocket#accept?
What I mean is: if I have a loop accepting connection it will be blocked here:

Socket client = serverSocket.accept();

If I catch an IOException during accept does this mean that I should close the server socket i.e. it is something severe or I e.g. log the exception and go back accepting connections in serverSocket.accept() like nothing happened?I.e. the server socket is full functional.

3 Answers 3

2

You can get these from running out of file descriptors. Another possibility is SocketTimeoutException, if you have set a timeout and it expires. Another is that the server socket got closed asynchronously. Only the last of these is necessarily fatal.

13
  • So you suggest to try to reaccept unless I see that the socket has been closed?
    – Cratylus
    Feb 20, 2013 at 20:23
  • @Cratylus Please let's not talk about reconnecting when we should be talking about accepting connections.
    – user207421
    Feb 20, 2013 at 20:24
  • Ok!So I should just go back into serverSocket.accept() unless in the catch of IOException the serverSocket.isClosed() returns true?
    – Cratylus
    Feb 20, 2013 at 20:30
  • It depends. If you've run out of descriptors you have to close one. If you got a timeout you need to consider why you set it.
    – user207421
    Feb 20, 2013 at 20:31
  • When you say descriptors you mean client sockets?
    – Cratylus
    Feb 20, 2013 at 20:36
1

I have seen only one case where accept was throwing an exception: too many open files. Depending on your aplication, you may wait and go into accept again, close try the loop of close/bind/accept or fail. We have implemented the both strategies an at the and it always endup with fail. Again, if your clients are short living, you may recover. Our clients long living...

15
  • 1)too many open files Do you mean too many client sockets?2)Did not understand what are the 2 strategies you suggest.a)go back to accept? b) close and recreate socket? something else?
    – Cratylus
    Feb 20, 2013 at 20:21
  • 1. yes, we have too many connections ~8000 and in node not configured correctly the you are in a trouble. 2. right. this is what we do shortly wait and try accept again and recreate socket.
    – kofemann
    Feb 20, 2013 at 20:24
  • If you run out of file descriptors you have to close something before you retry, unless you just want the same error immediately again. And again. And again. And just closing the ServerSocket and opening a new one doesn't release any descriptors.
    – user207421
    Feb 20, 2013 at 20:28
  • 2
    @Cratylus That's not what he said. I don't know where you get these strange ideas, or how you form these 'so' inferences that don't follow from anything that had been said.
    – user207421
    Feb 20, 2013 at 20:49
  • 1
    @Cratylus Connected to whatever they were connected to when it was still open. They are unaffected by closing it. Nothing happens to them. They stay connected. They don't hang. How many times, and how many ways, do you need to be told?
    – user207421
    Feb 20, 2013 at 20:57
-1

From the API doc:

IOException if an I/O error occurs when waiting for a connection.

This means that if exception was thrown here something is going bad. For example port is busy, network adapter is unavailable etc. So, try to wait a little bit and reconnect. If this happens again, nothing to do. You cannot connect to chosen port.

Change port? Change address? Close application with error message? :(

2
  • So, try to wait a little bit and reconnect.What do you mean by reconnect?Also port is busy would be a BindException not an IOException
    – Cratylus
    Feb 20, 2013 at 19:58
  • You've misunderstood both the Javadoc and the question. It is about ServerSocket.accept(). Changing ports etc does not solve this problem.
    – user207421
    Feb 20, 2013 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.