TCP Keepalive HOWTO |
Prev |
|
Next |
4. Programming Applications
This section deals with programming code needed if you want to the Create applications that use KeepAlive. This is not a programming manual, and it requires so you have previous knowledge in C programming and in Networking Conc Epts. I consider you familiar with sockets, and with everything concerning the general aspects of your application.
4.1. When your code needs keepalive support
Not all network applications need keepalive support. Remember that it's TCP keepalive support. So, as can imagine, only TCP sockets can take advantage of it.
The most beautiful thing if writing an application are to make it as customizable as possible, and not to Forc E decisions. If you want to consider the happiness of your users, you should implement keepalive and let the users decide if they want To use it or not by using a-configuration parameter or a switch on the command line.
4.2. ThesetsockoptFunction call
All your need to enable keepalive for a specific socket are to set the specific socket option on the socket itself. The prototype of the function is as follows:
setsockopt (int s, int level, int optname, const void *optval, socklen_t optlen) |
The first parameter is the socket, previously created with the socket(2); the second one must be SOL _socket, and the third must be so_keepalive . The fourth parameter must be a Boolean integer value, indicating that we want to enable the option while the Size of the value passed before.
According to the manpage, 0 is returned upon success, and -1 are returned on error (and errno are properly Set).
There is also three other sockets options you can set for keepalive if you write your application. They all with the sol_tcp level instead of Sol_socket, and they override system-wide variables only for t He current socket. If you read without writing first, the current system-wide parameters would be returned.
tcp_keepcnt: Overrides tcp_keepalive_probes
tcp_keepidle: Overrides tcp_keepalive_time
TCP_KEEPINTVL: Overrides TCP_KEEPALIVE_INTVL
4.3. Code examples
This was a little example that creates a socket, shows that keepalive was disabled, then enables it and checks that the Opti On is effectively set.
/*---Begin of keepalive TEST program---*/#include <stdio.h> #include <stdlib.h> #include <uni std.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h>int main (void); int Main () {int s; int optval; socklen_t Optlen = sizeof (optval); /* Create the socket */if ((s = socket (pf_inet, Sock_stream, ipproto_tcp) < 0) {perror ("socket ()"); Exit (Exit_failure); }/* Check the status for the keepalive option */if (getsockopt (S, Sol_socket, So_keepalive, &optval, &optlen) < 0) {perror ("getsockopt ()"); Close (s); Exit (Exit_failure); } printf ("So_keepalive is%s\n", (optval?) "On": "OFF")); /* Set the option active */optval = 1; Optlen = sizeof (optval); if (setsockopt (S, Sol_socket, So_keepalive, &optval, Optlen) < 0) {perror ("setsockopt ()"); Close (s); Exit (Exit_failure); } printf ("So_keepalive set on socket\n"); /* Check the status again*/if (getsockopt (S, Sol_socket, So_keepalive, &optval, &optlen) < 0) {perror ("getsockopt ()"); Close (s); Exit (Exit_failure); } printf ("So_keepalive is%s\n", (optval?) "On": "OFF")); Close (s); Exit (exit_success);} /*---End of keepalive test program---*/ |
Prev |
Home |
Next |
Using TCP keepalive under Linux |
|
Adding Support to Third-party software |
KeepAlive Support-----Programming applications