Server Side socket connection closes after few minutes of inactivity

Hello,
My server can handle multiple clients using TCP sockets.
A worker thread listens and accepts new connections then add it to array
SOCKET clientsocket[100]
Problem I am facing is that if there are few minutes of inactivity with specific client I cannot connect with it (Client program keeps waiting to recv() data as its in blocking mode) but the data sent by server never gets to client.

I have read that usually such timeout is 2 hours, I have also read about Keep alive parameter but how is it possible that my connection closes after few minutes of inactivity?

Thanks
Last edited on
A worker thread listens and accepts new connections ...
That doesn't sound quite right. And why do you need an array of clients anyway?
The thread keeps accepting new connections and add each to array
1
2
3
4
5
6
7
8
while(1)
{
clientsocket[i] = accept(listening, (sockaddr*)&client, &clientsize);
.
.
.
i++;
}

I am adding to array so I can process any selected client. For example if 3 clients are added and I want to access 2nd I can send data to clientsocket[1]..
I have seen similar implementation on various forums hope there is nothing wrong with it.
Thanks for responding
Last edited on
How do you cope with a client disconnecting?
As an immediate result, I am sending heart beat signal to idle clients every 2 minutes.
Is that a good solution?
If you think something is timing out the connection, then that's one way to deal with it. However, you really only need that stuff with UDP connections, TCP has all that stuff built in (that's kinda the point of TCP), so there's something somewhere killing the connection if you're getting that with TCP.
Server is built on visual studio c++ MFC, something is definitely killing the connection. Could it be because of the VPN I use? because when I connect to clients locally (this issue does not appear).
What is a good way to find out?
Thanks
It won't be the VPN itself, VPNs a designed to be reliable. But "Ops" love to kill things, long running processes, long connections, ... Typically, they don't want anything abusing their lovely systems.

You'll have to make the clients reconnect.
Cool thanks for the help.
I tested using various connections, and seems like its the operating system after all trying to save resources and cutting idle connections.
To keep connections alive, just send a heart beat ;)
Topic archived. No new replies allowed.