0

I'm using Java and Netty to connect to a Unity-Client through a TCP/IP connection.

Sometimes the connection can break and the Unity-guy proposed to send a ping (just a one byte message) every 5 seconds and if the time of reception of the last ping is higher than 30 seconds, the connection is lost.

Do I really need to mesure the time? Isn't it enough to trigger an exception if I try to send something through the socket? Is there a case where a write won't detect a connection loss?

I tagged it Netty since I am using it, but it probably is the same with a simple Socket connection.

Burkhard
  • 14,596
  • 22
  • 87
  • 108
  • 1
    No it isn't enough. TCP has buffering at both ends, which means that any specific write won't fail at all: it is the subsequent writes that will fail. – user207421 Nov 02 '16 at 09:20

1 Answers1

0

I think the real answer is: you have to define for yourself how you want your application to behave.

What I mean is: there are many different problems that could occur to your connection. Writing data, and failing immediately when that doesn't work is just one option to handle that.

You can also envision continuous pinging; for example to detect when the other is just "temporarily" unavailable. In other words: you establish some sort of "heartbeat" monitoring; and thus you enable your application for a "wider range" of potential "reactions".

GhostCat
  • 137,827
  • 25
  • 176
  • 248