1

I'm currently working with sockets in Java. I have created a listener which will connect to a ServerSocket. This works perfectly fine. However, if my application may encounter unexpected closure, the sockets won't be closed properly. Upon the next run, this will throw a SocketException: socket closed on socket = serversocket.accept();

However, by testing with serversocket.isBound() I've discovered that the serversocket is in fact bound and therefor not closed. Is there any way for me to determine whether it is possible for me to accept the connection and if not, to clear the socket and accept after this?

Thanks in advance.

Menno
  • 12,175
  • 14
  • 56
  • 88
  • You can check if the specified port is available - different methods possible - see if this method is of any help http://stackoverflow.com/a/435579/1007273. Also check out how to properly close sockets in Java here on stackoverflow. Maybe you have to also consider defining some reasonable timeouts for your connections. Good luck! – hovanessyan Jan 04 '13 at 15:32
  • I know how to properly close it, this is more of a case for a unexpected end. Will check it out right away! – Menno Jan 04 '13 at 15:39
  • well, you can always use try/catch/finally blocks, and call your closing methods in the finally block, ensuring it will always execute. – hovanessyan Jan 04 '13 at 15:52
  • Well, say the 'unexpected' end is a re-deployment of the application. Would the finally case be triggered upon such an event? – Menno Jan 04 '13 at 16:39
  • I don't know. Probably that's up to your app/web server implementation. Though, I think you can craft some code to test that behavior. – hovanessyan Jan 04 '13 at 16:49

1 Answers1

1

However, if my application may encounter unexpected closure, the sockets won't be closed properly. Upon the next run, this will throw a SocketException: socket closed on socket = serversocket.accept();

You have that back to front. 'Socket closed' means the socket was closed, not that it wasn't.

However, by testing with serversocket.isBound() I've discovered that the serversocket is in fact bound and therefor not closed.

Wrong. isBound() tells you whether you ever called bind(), explicitly or implicitly. It doesn't change what it returns after you close the socket. Try isOpen() or isClosed(), whichever it is, if you want to know whether the socket is still open.

You need to believe what the exception is telling you. You closed the server socket so now you can't accept from it.

Is there any way for me to determine whether it is possible for me to accept the connection and if not, to clear the socket and accept after this?

It would be more to the point to fix the bug whereby the socket ever got closed in the first place.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • `if(!socket.isClosed()) socket = serversocket.accept();` will still throw `java.net.SocketException: socket closed`. So my socket isn't closed, but it is, somehow. By the way, this occurs only after a sudden shutdown, for instance redeployment (just for testing purposes). – Menno Jan 05 '13 at 06:29
  • 1
    @Aquillo That must mean that another thread is closing the server socket while this one is blocked in accept(). – user207421 Jan 05 '13 at 18:15