2
\$\begingroup\$

I have a head-to-head game where two players communicate through a server based on TCP.

I am planning to implement a ping/pong mechanism in order to detect network disconnection situations. For example,

A pings B every 500ms. If A receives no response from B, A shall consider B is disconnected. If A receives no response from the server, A shall consider A is disconnected itself.

Do you think this ping/pong system is needed or other method is recommended?

\$\endgroup\$
1
  • 2
    \$\begingroup\$ You say they talk through a server, but then you say they ping each other. (Through the server?) Please clarify your network architecture in the question. \$\endgroup\$
    – John K
    Mar 17 '15 at 12:19
1
\$\begingroup\$

There is no need to ping if you are using TCP. TCP has built in mechanisms for detecting disconnects, congestion, or easily deriving latency.

To elaborate:

  • the Keep Alive feature sends small packets during idle times to detect disconnections.
  • The TCP header contains sequence numbers and ACK numbers that exactly correspond to each other, so you can measure the difference in time between the two to easily derive RTT (round-trip time)
\$\endgroup\$
2
  • \$\begingroup\$ The recommended value for keep alive for TCP is quite large. I am not sure if it can be used if I want to detect the disconnection within seconds. \$\endgroup\$
    – Joe Huang
    Mar 17 '15 at 6:42
  • \$\begingroup\$ Ben Noordhuis says: "Still, [TCP Keep Alive] won't let you magically detect a severed connection at the time of the disconnect itself because that's simply not how TCP works." \$\endgroup\$
    – Anko
    Mar 17 '15 at 11:23
1
\$\begingroup\$

It's not necessary in TCP as, as @congusbongus said, TCP can automatically detect disconnects.

But doing it your self has the extra advantage of being able to know how much lag there is on the server, as well as being able to give advanced warning of the problem while TCP is still trying to reconnect.

All in all, I'd say add pings, they're not difficult to implement and you'll be able to show how much lag players are having.

\$\endgroup\$
1
  • \$\begingroup\$ When I manually close Wifi in my computer, actually TCP will not detect it as disconnection. So I think ping/pong is still needed for this case no matter how. \$\endgroup\$
    – Joe Huang
    Mar 28 '15 at 6:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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