0

正如我在标题中提到的,我正在使用 keepalive 选项来检测服务器端的死客户端。在连接的 tcp 套接字上启用 keepalive 的代码片段如下所示。其他操纵 keepalive 行为的选项,如 TCP_KEEPCNT、TCP_KEEPIDLE、TCP_KEEPINTVL 等,默认情况下是系统。

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”中读取它并确保客户端已死。谢谢。

4

1 回答 1

0

当 keepalive 检测到死客户端时,recv 会返回什么?

-1 具有适当的值errno.

或者它是否为系统错误变量设置了错误

是的。

于 2013-10-30T09:14:14.920 回答