I have a server-client program. I want my server to detect if one of my clients disconnects (unplugged client cable) and delete that client from its list. I used this code (working on Linux) : in QThread:
m_sock = new QTcpSocket();
m_sock->setDescription(ds);
int enableKeepAlive = 1;
int fd = m_sock->socketDescriptor();
setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &enableKeepAlive, sizeof(enableKeepAlive));
int maxIdle = 10; /* seconds */
setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &maxIdle, sizeof(maxIdle));
int count = 3; // send up to 3 keepalive packets out, then disconnect if no response
setsockopt(fd, SOL_TCP, TCP_KEEPCNT, &count, sizeof(count));
int interval = 2; // send a keepalive packet out every 2 seconds (after the 5 second idle period)
setsockopt(fd, SOL_TCP, TCP_KEEPINTVL, &interval, sizeof(interval));
It works on client when I disconnect the network cable (timeout error appears). But nothing happens on server side (I used same code for both).