The default is set to 2 hours. How do you change it?
We have an application that handle mutiple TCP clients. If the client get reseted the server don’t get notify. I’m trying to setup a hearbeat message to close the inactive socket. 2 hours is kinda too long , i’m looking for something like 30 sec heartbeat.
The best is to have the hearbeat data go in both direction. The server and client send data to each other every x seconds, what ever you need to comply with the requirement. That way server can detect if client is dead and client can detect if server is dead.
I think the TCP_KEEPALIVE option also exist in 4.25 TCPIP (can’t say for sure which version of TCPIP though).
You create a socket(), set the SO_KEEPALIVE on the socket (if you are accepting, make sure you do this on the accept socket, not the listen one). And you can also set the TCP_KEEPALIVE on the socket, to set the idle time.
Note you can change that 2 hours to any seconds you want, but once connection failed, it still took TCP about 10 minutes to decide and close the connection.
So I’m gonna stick with my implementation. If the client want keep their connection alive then they will need to send at least 1 caractere before the timeout expired.
Typically what I do on the server side of things is to set a max number of clients I expect to handle (say 10).
As each client connects and sends messages I timestamp them (the connection time and each message received time).
When all 10 connections are filled, on the 11th connection request I go through my list of 10 connections and close the oldest one (presumably it’s a dead one). Also, if you only expect 1 connection per client, you can easily check and see if the IP of the incoming connection matches any in your currently connected list. If it does, you can close the old one (that’s dead) and accept the new one into it’s place.
Then you don’t have to rely on clients sending keep alives.
Tim,
Thanks for sharing your implementation. Actually i’m using a counter for each socket and when it reach the max then I close it.
But the one thing I didn’t really figure is how to limit the number of socket. For our application we may have between 20-100 sockets which is variable depending of the sites.
I tried to set listen to 0 instead of 5 to stop our socket to listen for connection if reach max (100) but it doesn’t work. Client still get queue and they don’t receive a message like connection refused.
I have also try the function shutdown but I can’t restart my socket to listen.
So that could be a nub question but how do you stop your socket to listen temporaly?
May be I’m missing something but the following didn’t work for me
2 sockets left = Listen(2)
0 sockets left = Listen(0)