4

How can I check if my connection with the server lost ? (Client or Server side no matter) I'm using TCP connection, and the server recv unlimit clients. for each client the server create thread. and with that's way I can recv / send for each client.

Ido Hadar
  • 179
  • 3
  • 5
  • 13
  • Explain what you mean by 'lost'? Either side closes the connection or do you want to set some sort of response timer after which you consider the connection to be 'lost' ? – Lieuwe Mar 07 '13 at 13:44
  • Then the connection lost, I wanna try connect again. Do that till I'll connect. and when I conncted and the connections lost again. Retry to connect. – Ido Hadar Mar 07 '13 at 13:48

4 Answers4

4

You could consider using the TCP-keepalive option on the socket as one option.

But many NATs and statefull firewalls will actually drop TCP connections if it observes no activity within a certain period of time.. And this activity timeout may be faster than the periodic keepalive message supported by TCP. For this reason, a protocol message to your server every 30-60 seconds is usually enough to keep the connection "alive" with regards to NATs.

My personal take is this. It's the responsibility of the client to inform the server that "I'm still here" if the connection is meant to be long running. The server should periodically wake up and close any TCP connection that hasn't had any traffic on it the last N seconds. (Where N is a reasonable value for your application). In your case, the "thread per client" approach means each thread just needs to decide when a remote client has become unresponsive.

Within the protocol, the client can send it's own custom "ping" message to the server every 30-60 seconds. The server always acks this message back and records that the client is still around.

A lot of what I'm discussing really depends on your protocol, what your service does, and how long the connections are expected to last.

selbie
  • 100,020
  • 15
  • 103
  • 173
  • Figuring out if the TCP connection is still active is not the programmers responsibility. Keeping it alive is. +1 for this post. – Klaas van Gend Mar 08 '13 at 11:26
1

Build a ping system in your application.

tr0yspradling
  • 529
  • 1
  • 9
  • 20
0

If a socket is not connected anymore, send() returns SOCKET_ERROR and WSAGetLastError() returns WSAENOTCONN or other related error.

If you are reading data and recv() returns zero, then the peer has performed an orderly shutdown.

If you are reading from a not connected socket, then recv() returns SOCKET_ERROR and WSAGetLastError() returns WSAENOTCONN or other related error.

WinSock error codes are documented on MSDN.

Davide Berra
  • 6,387
  • 2
  • 29
  • 50
  • There's a difference between a 'not connected socket', which is just a socket you haven't called connect() on yet, which causes ENOTCONN, and a socket whose connection is broken, which will cause either an EOS when reading, or ECONNRESET or similar when reading or writing. – user207421 Mar 07 '13 at 22:48
  • @EJP - "How can I check if my connection with the server lost", I assume the connection was established. Therefore i don't get what's wrong with my answer – Davide Berra Mar 08 '13 at 08:24
  • What is wrong with your answer is that `ENOTCONN` is only returned in case (1) when the connection had never been established, and never in case (2) when the connection had been established. I don't know how many different ways you want me to say this. – user207421 Mar 27 '13 at 06:21
  • Mr. @EJP: "I don't know how many different ways you want me to say this" <- we don't need arrogance, thank you. – Davide Berra Mar 27 '13 at 08:21
0

Keep a async or sync receive call outstanding at all times (many apps do this anyway by design) - on connection loss, this will complete with failure conditions depending on your specific receive API.

Steve Townsend
  • 53,498
  • 9
  • 91
  • 140