2

Please ignore this question which has been requested to be deleted by the author. Please see the comments for the reason.

The following code always throws

java.net.SocketException: Socket closed

at the last statement (socket.connect(myInetSocketAddress, 6000);)

Socket socket = new Socket();
socket.connect( myInetSocketAddress, 6000);

//use socket to do some reading
...

socket.close();
socket = new Socket();
//The following is bound to throw java.net.SocketException: Socket closed
socket.connect(myInetSocketAddress, 6000);

If waiting (Sleep()) is added, everything will be fine as following:

Socket socket = new Socket();
socket.connect( myInetSocketAddress, 6000);

//use socket to do some reading
...

socket.close();
Thread.sleep(1000);  //critical to prevent java.net.SocketException: Socket closed
socket = new Socket();
//The following works fine after Thread.sleep(1000)
socket.connect(myInetSocketAddress, 6000);

Could anyone shed some light on this?

Hong
  • 17,643
  • 21
  • 81
  • 142
  • This is not the real code. You did not get that that exception from that code. – user207421 May 16 '15 at 20:48
  • @EJP I just refreshed Java socket code and it seems that you might be right. – Mateusz May 16 '15 at 22:18
  • @EJP Please see my comment following your answer, and feel free to let me know how I can clarify my post to convince you that the last statement of the code throws the exception. – Hong May 16 '15 at 22:30
  • @Hong You can't convince me. The result of `new Socket() `is not a closed socket. It can only throw the 'socket closed' exception if you try to re-connect the old socket, instead of creating a new one and connecting that. – user207421 May 17 '15 at 02:38
  • I think you may be right. The code is almost identical to the real code, and th exception is caught exactly at the place I described. However there is another thread using socket, and may be closing the socket. I will have to look into it tomorrow. It is very late here. – Hong May 17 '15 at 03:24
  • Multiple threads accessing the same socket is a problem anyway. At the least you should be synchronizing. – user207421 May 17 '15 at 07:20
  • @EJP, I have finally straightened out the issue. To put a long story short, socket is a class scope variable, and the omitted code (...) in the post includes another thread doing all the reading. The condition that triggers the closing of socket also triggers the cleanup of that thread before it is terminated. The cleanup out of caution also tries to close the this socket, and that caused the problem. Apparently it closes within 1 second, and this is why the 1 second sleep made the difference. Thank you for your persistent help. Should I delete this question in light of this? – Hong May 19 '15 at 09:49
  • I think so. You should have synchronized all access to the socket, as I said above, then the problem would not have arisen. Adding sleeps to networking code is just cargo-cult programming. It doesn't solve anything, it just moves the problem around. – user207421 May 19 '15 at 11:59

1 Answers1

1

Why is waiting required after a socket is closed before re-connection?

It isn't.

It isn't the sleep that made any difference here, it is the new Socket() line.

Clearly your first piece of code isn't an accurate representation. You got SocketException: socket closed, which indicates that you were trying to re-connect the original socket, so clearly you didn't have the new Socket() line when you ran it.

This also explains why calling setReuseAddress() didn't make any difference. That would only help with a BindException, and you would only get that if you were trying to connect using the same local IP address and port, using the 4-argument connect(), which you weren't doing.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Sorry if my code has confused you. Please feel free to let me know how I can clarify it. Is my first sentence about the last statement throwing the exception confusing? Maybe I have not understood your answer. – Hong May 16 '15 at 21:34
  • @Hong Just take out the sleep and leave the second `new Socket()` line. You will find that the problem has magically disappeared. – user207421 May 17 '15 at 01:38
  • 1
    Actually this is the correct answer and not the one marked as answer. – Philip Stuyck May 17 '15 at 09:10