A few of the common socket options are:
SO_REUSEADDR:
This is to report whether the same port can be reused again that is socket can be bind to the same port or not. This is a Boolean option.
SO_KEEPALIVE:
This is to report whether the keep-alive message is flowing between client and server that connections are kept active with the periodic transmission of the message. This is a Boolean option.
SO_SENDBUF:
This reports the send buffer size information.
SO_RECVBUF:
This reports the received buffer size information.
SO_TYPE:
This reports the type of socket.
SO_DONTROUTE:
This reports whether the message can pass through the router.
These options can be manipulated by two system call API:
- getsockopt()
- setsockopt()
getsockopt():
int getsockopt (int sock_fd, int level, int options, void* optval, socklen_t *len);
- With this API, the current value of the options for the given socket can be fetched.
- The first parameter is the socket descriptor.
- The level argument specifies the protocol at which the options resides. To retrieve the socket level option, specify the argument as SOL_SOCKET, for TCP and UDP,level can be set to IPPROTO_TCP and IPPROTO_UDP respectively.
- The option argument specifies the option for which the current value to be fetched.
- The fourth argument is the pointer to variable which holds the current value for the option.
- The length field, initially is the size of buffer pointed by optval and later modified to indicate actual size of optval on return.
- On success it returns 0 and -1 on failure.
setsockopt():
int setsockopt (int sock_fd, int level, int options, const void * optval, socklen_t optlen)
- With this API, the current value of the options for the given socket can be set to the new value.
- The first parameter is the socket descriptor.
- The level argument specifies the protocol at which the options reside. To retrieve the socket level option, specify the argument as SOL_SOCKET, for TCP and UDP, the level can be set to IPPROTO_TCP and IPPROTO_UDP respectively.
- The option argument specifies the option for which the current value to be set.
- The fourth argument is the pointer to a variable that holds the new value for the option.
- The length field is the actual size of optval to set.
- On success it returns 0 and -1 on failure.
Sample Program:
int optval; unsigned int len = sizeof(optval); int sock_fd = socket (AF_INET, SOCK_STREAM, 0); if (sock_fd == -1) { printf ("\n Socket creation failed %d %s \n", errno, strerror(errno)); return -1; } /* Getting the value of Keepalive */ int ret = getsockopt (sock_fd, SOL_SOCKET, SO_KEEPALIVE, &optval, &len); if (ret == 1) { printf("\n Fetching value for keepalive option failed\n"); return -1; } printf ("SO_KEEPALIVE is %s\n", (optval ? "ON" : "OFF")); optval = 1; /* Setting the value of Keepalive */ ret = setsockopt (sock_fd, SOL_SOCKET, SO_KEEPALIVE, &optval, len); if (ret ==1) { printf("\n Fetching value for keepalive option failed\n"); return -1; } printf ("SO_KEEPALIVE is %s\n", (optval ? "ON" : "OFF"));
Relevant Posts:
- Sockets Introduction
- Socket APIs
- TCP Socket Program
- UDP Socket Program
- Multi-client server support- select() API
- Host and Network Byte ordering
- Little-endian and big-endian
Categories: Operating system (OS)
Leave a Reply