Question

As I have mentioned in the title, I am using keepalive options to detect dead client on server side. The code snippet that enables keepalive, on the connected tcp socket, looks alike as below. Other options which manipulates keepalive behaviour like TCP_KEEPCNT, TCP_KEEPIDLE, TCP_KEEPINTVL etc, are systems by default.

int optval;
socklen_t optlen = sizeof(optval);
if(setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen) < 0) {
  perror("setsockopt()");
  close(s);
  exit(EXIT_FAILURE);
}

On the server side, on the connected socket, server thread will be in the recv call, waiting for client to send commands to server. The code snippet for this looks something like this.

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;
            }
}

When client does orderly shutdown, recv call returns 0 and I make sure that client has terminated. So my question is What does recv return when keepalive detects dead client? or does it set a error to system error variables, which I can read it from "get_last_sock_error" and make sure that client is dead. Thanks.

Was it helpful?

Solution

What does recv return when keepalive detects dead client?

-1 with an appropriate value for errno.

or does it set a error to system error variables

Yes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top