How To Use TCP KeepAlive?

This page answers questions like these:


Related Links:
What Is TCP KeepAlive?
What Are The Default TCP KeepAlive Parameters?
TCP KeepAlive Parameter Ranges
How Do I Check That TCP KeepAlive Is Working?
Ephemeral Ports



How Do I Use TCP KeepAlive?

The example code below can be used in either C++ or C to enable (or disable) KeepAlive for a socket connection in one direction. If running this code on the server side, you should run it on the new socket that comes back from the accept() system call. This is safer than running it on the listening socket and assuming that KeepAlive is inherited across the accept() system call because on some systems it isn’t.

#include <sys/socket.h> /* socket(), listen(), accept(), setsockopt(), SOL_SOCKET, etc. */ #include <netinet/in.h> /* IPPROTO_TCP, etc. */ #include <netinet/tcp.h> /* TCP_KEEPIDLE, etc. */ /* ** Your own code to create a socket, as a client using socket(), ** or as a server using accept(), is needed here ... */ int socket; ... /* ** Now we enable KeepAlive for the socket ... */ int switch; /* 1=KeepAlive On, 0=KeepAlive Off. */ int idle; /* Number of idle seconds before sending a KeepAlive probe. */ int interval; /* How often in seconds to resend an unacked KeepAlive probe. */ int count; /* How many times to resend a KA probe if previous probe was unacked. */ /* Switch KeepAlive on or off for this side of the socket. */ if (setsockopt(socket, SOL_SOCKET, SO_KEEPALIVE, &switch, sizeof(switch)) < 0) { /* Error occurred, so output an error message containing: __LINE__, socket, SO_KEEPALIVE, switch, errno, etc. */ } if (switch) { /* Set the number of seconds the connection must be idle before sending a KA probe. */ if (setsockopt(socket, IPPROTO_TCP, TCP_KEEPIDLE, &idle, sizeof(idle)) < 0) { /* Error occurred, so output an error message containing: __LINE__, socket, TCP_KEEPIDLE, idle, errno, etc. */ } /* Set how often in seconds to resend an unacked KA probe. */ if (setsockopt(socket, IPPROTO_TCP, TCP_KEEPINTVL, &interval, sizeof(interval)) < 0) { /* Error occurred, so output an error message containing: __LINE__, socket, TCP_KEEPINTVL, interval, errno, etc. */ } /* Set how many times to resend a KA probe if previous probe was unacked. */ if (setsockopt(socket, IPPROTO_TCP, TCP_KEEPCNT, &count, sizeof(count)) < 0) { /* Error occurred, so output an error message containing: __LINE__, socket, TCP_KEEPCNT, count, errno, etc. */ } }



Related Links:
What Is TCP KeepAlive?
What Are The Default TCP KeepAlive Parameters?
TCP KeepAlive Parameter Ranges
How Do I Check That TCP KeepAlive Is Working?
Ephemeral Ports

Home  >  C++ / C  >  How To Use TCP KeepAlive?


Tags: KeepAlive example code, keep alive code example, tcpip keepalive, tcp keepalive socket, setsockopt, socket, TCP_KEEPIDLE, TCP_KEEPINTVL, TCP_KEEPCNT, IPPROTO_TCP, SOL_SOCKET, SO_KEEPALIVE, TCP, Transmission Control Protocol, C++, C

Copyright © HelpDoco.com
tcp-keepalive-how.txt
C++-C/how-to-use-tcp-keepalive.htm
1