1

I'm using a (Java) TCP socket to connect to a network-enabled device.

Think about the following scenario:

1) TCP socket connection successfully created

2) Network connection interrupted for a short time (I'm testing this by simply unplugging the network cable)

3) My program closes the socket.

4) The other side of the socket only responds to requests from my side; thus, the socket at the other end of the connection does not necessarily detect the broken connection

5) Network connection is reestablished

6) My program tries to open the socket again

7) -> Get a SocketException: connection refused

Why?

Because the socket at the other end thinks that the tcp connection is still open and thus refuses any other connection requests at the same port.

What happens next?

After about 5 minutes, my program is able to open the socket again, because the other end detected that the connection is not active anymore.

Question:

Is there any way to reduce the time until I'm able to reconnect again? I'm not able to make any changes on the "other" site, i.e., I can't change the tcp connection handling of my network enabled device.

mort
  • 12,988
  • 14
  • 52
  • 97
  • 1
    If you can't make changes to "the other side", I don't like your chances. If anyone answers this though it should be interesting. – Kevin D Feb 10 '12 at 12:07
  • @Gagandeep Bali: According to Javadoc, the SocketTimeout is for reading data from a socket only: http://docs.oracle.com/javase/1.4.2/docs/api/java/net/Socket.html#setSoTimeout(int) – mort Feb 10 '12 at 12:17
  • @Gagandeep Bali: I don't see how SO_LINGER would help to notify the other side that the connection broke. – mort Feb 10 '12 at 12:33

1 Answers1

0

Without being able to change how the TCP connection is handled on the other side, there is unfortunately nothing much you can do in this scenario. This is a common issue with Java sockets in general.

Community
  • 1
  • 1
Marvin Pinto
  • 30,138
  • 7
  • 37
  • 54
  • Thanks for your answer. You say it's a common issue with Java sockets, is there a solution for e.g., unix sockets? – mort Feb 10 '12 at 12:11
  • 1
    @mort I suspect this problem isn't _as_ pronounced with UNIX sockets in general, but I don't know how that would apply to your scenario (not knowing _what_ the other device is, _how_ the sockets are handled, etc). – Marvin Pinto Feb 10 '12 at 12:16
  • @mort The "workaround" in situations like this (specifically with Java sockets now), is to attempt to write to it and if/when an exception is thrown, you know that socket connection is closed. Since you don't have access to change this on the _other_ side, I'm not sure what you can do. – Marvin Pinto Feb 10 '12 at 12:18
  • About the workaround: that's what I'm doing already at my side of the connection. I thought that there should be some way to tell the "other side" to detect that the socket connection is broken. – mort Feb 10 '12 at 12:21
  • Anyway, your links are a really good explanation of the problem! – mort Feb 10 '12 at 12:22
  • @mort Yeah that's the thing, the _other_ side needs to know that the socket is no longer alive. Now _not_ knowing what the device is, here's what you can try: Look into sending a `RST` signal right before you kill your application. You'll have to do this on the lower levels as I'm pretty sure Java can't. – Marvin Pinto Feb 10 '12 at 12:24
  • @mort If you can pull this off _somehow_, then I suspect you won't have this _socket timeout_ issue anymore. But it will be tricky because you have to find a way to send this signal _right before_ you pull the plug (in your Java program), and I don't know how you'll implement that :) – Marvin Pinto Feb 10 '12 at 12:26
  • "But it will be tricky because you have to find a way to send this signal right before you pull the plug" - that won't be possible since the assumption is that the connection somehow breaks (maybe physically, maybe the DHCP assigns a new IP to the device, maybe the network is overloaded) – mort Feb 10 '12 at 12:29
  • @mort Yep, exactly :) Hence why I'm not sure what you _can_ do given that you're not able to change how the _other_ side works. – Marvin Pinto Feb 10 '12 at 12:31
  • 1
    It's not a big issue since the other side will finally detect that the socket connection broke. I was just trying to find out if there was an easy way to do this. Since there doesn't seem to be one, I'll accept your answer. Thanks for your efforts! – mort Feb 10 '12 at 12:34
  • "This is a common issue with Java sockets in general": no. This is a common issue with *TCP sockets* in general. It has nothing to do with Java whatsoever. – user207421 Feb 13 '12 at 09:23