0

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).

  • 1
    what exactly happens on the client side? does a call to `recv()` return -1? Also, why are you using native socket calls? why don't you use `QTcpServer` and `QTcpClient`? `QTcpServer` and `QTcpClient` are better suited to the Qt event loop, they can provide signals like [`disconnected()`](https://doc.qt.io/qt-5/qabstractsocket.html#disconnected). This can be better than having a whole thread just blocking on a `recv()` call to make sure the connection is still going. – Mike Sep 21 '16 at 13:43
  • The bottom line is: when client or server unplug the network cable, QT can not detect. I need to use OS dependent API to do this. – Ngọc Tân Đỗ Sep 22 '16 at 03:47
  • That's not true, You can use Qt to detect when the client or the server unplug the network cable, I've written a similar thing before. I would suggest to implement your own heartbeat protocol, have a look at [this question](https://stackoverflow.com/q/35699474/2666212). – Mike Sep 22 '16 at 10:00
  • Also, Have you tried waiting after removing the client's cable for some time? I think that the keep alive interval is not being applied, try waiting for 5 minutes after removing the client's cable, and see if the servers detects anything. – Mike Sep 22 '16 at 10:13
  • I'm implementing heart-beat solution. With the keep-alive, I used the same code in both client and server. The Client is ok (If I remove the cable of server) . With the server: I wait more than 30 minutes and it does not make sense. In server side: I create a socket in a QThread and set keep-alive atfer that. May be, is this the problem ? – Ngọc Tân Đỗ Sep 22 '16 at 10:54
  • How do you *create a socket* in the server side? you should be using the socket returned from `accept` and pass that to the `QThread` – Mike Sep 22 '16 at 11:01
  • When Qthread start(), I create a new QTcpSocket.That's all – Ngọc Tân Đỗ Sep 22 '16 at 11:07
  • Then, please post all the relevant parts of code, as I thought you were using only native socket calls. Although, Implementing your own heartbeat should be better and more portable, so I advise you to stick to that. – Mike Sep 22 '16 at 11:19
  • I added a small description . It looks like that ...... Anyways, I will try to test the heart-beat tomorrow. Thank you – Ngọc Tân Đỗ Sep 22 '16 at 11:37

0 Answers0