0
正如我在標題中提到的,我使用keepalive選項來檢測服務器端的死客戶端。在連接的tcp套接字上啓用keepalive的代碼片段如下所示。其他操作Keepalive行爲的選項,如TCP_KEEPCNT,TCP_KEEPIDLE,TCP_KEEPINTVL等,都是默認情況下的系統。如何使用keepalive選項啓用檢測死客戶端
int optval;
socklen_t optlen = sizeof(optval);
if(setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen) < 0) {
perror("setsockopt()");
close(s);
exit(EXIT_FAILURE);
}
在服務器端,在連接的套接字上,服務器線程將在recv調用中,等待客戶端向服務器發送命令。這段代碼片段看起來像這樣。
for(; dwBytesReceived < dwBlockSize; dwBytesReceived += iByteCount) {
retry:
iByteCount = recv(m_ConnectSocket, ((UX_CHAR *)pBlock + dwBytesReceived), (dwBlockSize - dwBytesReceived), flag);
if (UX_SUCCESS >= iByteCount) {
if (iByteCount == 0) {
// Connection from client is unexpectedly closed. Terminate Server instance thread.
ret = UX_ECLIENTSHUTDOWN;
UX_ERROR("Connection from client is unexpectedly closed. Terminating Server instance thread, errno:%d", UX_ECLIENTSHUTDOWN);
goto out;
}
ret = get_last_sock_error();
if (EAGAIN == ret) {
UX_ERROR("The socket is marked non-blocking and the requested operation, recv, would block");
goto retry;
}
ret = -errno;
goto out;
}
}
當客戶端按順序關閉時,recv調用返回0,並確保客戶端已終止。所以我的問題是當keepalive檢測到死客戶時recv返回什麼?或者它是否給系統錯誤變量設置了一個錯誤,我可以從「get_last_sock_error」中讀取它並確保客戶端已經死亡。謝謝。
爲什麼不嘗試呢?你不能在另一個盒子上連接客戶端,拔掉盒子並等待看看會發生什麼? –
我試過了,連接客戶端到服務器,然後斷開服務器的局域網電纜連接到網絡。 keepalive超時後,調用t0 recv()返回錯誤,並將errno設置爲110,即Connection超時。 –