sockets - SO_KEEPALIVE: Detecting connection lost or terminated -


i have multiple threads have socket open client application each. each of threads receive instruction main thread send commands client (commands run test, stop test, terminate session, exit....). threads generic, have socket per client , send command when main thread asks to.

the client exit or crash, or network bad.

i have been trying see how figure out tcp session has ended per client. 2 solutions have found seem appropriate here.

1) implement own heartbeat system 2) use keepalive using setsockopt.

i have tried 2) sounds faster implement, not sure of 1 thing: so_keepalive generate sigpipe when connection interrupted please? saw should case never received sigpipe.

this how 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 code accepts connection 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 test function:

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

test() never gets triggered. understanding wrong please? not sure if sigpipe gets generated or not. lot.

if keep-alive fails, connection invalidated os, , subsequent read/write operations on socket fail appropriate error code. need make sure reading/writing code handling errors can close socket, if not doing so.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -