user1777907
user1777907

Reputation: 1375

SO_KEEPALIVE: Detecting connection lost or terminated

I have multiple threads who have a socket open with a client application each. Each of those threads receive an instruction from a main thread to send commands to the client (commands could be run test, stop test, terminate session, exit....). Those threads are generic, they just have a socket per client and just send a command when the main thread asks it to.

The client could exit or crash, or network could be bad.

I have been trying to see how to figure out that my TCP session has ended per client. Two solutions that I have found that seem appropriate here.

1) Implement my own heartbeat system 2) Use keepAlive using setsockopt.

I have tried 2) as it sounds faster to implement, but I am not sure of one thing: Will SO_KEEPALIVE generate a SIGPIPE when connection is interrupted please? I saw that it should be the case but never received a SIGPIPE.

This is how my code looks:

void setKeepAlive(int sockfd) {
    int optval;

    optval = 1;
    setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof(optval));
    optval = 1;
    setsockopt(sockfd, SOL_TCP, TCP_KEEPIDLE, &optval, sizeof(optval));
    optval = 1;
    setsockopt(sockfd, SOL_TCP, TCP_KEEPCNT, &optval, sizeof(optval));
    optval = 1;
    setsockopt(sockfd, SOL_TCP, TCP_KEEPINTVL, &optval, sizeof(optval));
}

And my code that accepts connection is as follows:

for (mNumberConnectedClients = 0; mNumberConnectedClients < maxConnections; ++mNumberConnectedClients) {
    clientSocket = accept(sockfd, (struct sockaddr *) &client_addr, &clientLength);

    // set KeepAlive on socket
    setKeepAlive(clientSocket);

    pthread_create(&mThread_pool[mNumberConnectedClients], NULL, controlClient, (void*) &clientSocket);
}

signal(SIGPIPE, test);
....

And the test function:

void test(int n) {
    printf("Socket broken: %d\n", n);
}

test() never gets triggered. Is it my understanding that is wrong please? I am not sure if SIGPIPE gets generated or not. Thanks a lot.

Upvotes: 2

Views: 1011

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 596256

If a keep-alive fails, the connection will simply be invalidated by the OS, and any subsequent read/write operations on that socket will fail with an appropriate error code. You need to make sure your reading/writing code is handling errors so it can close the socket, if it is not already doing so.

Upvotes: 2

Related Questions