C Programming

Setsockopt Function in C

“A setsockopt() function is a very important concept related to networking in the c language. It is a very useful function related to the socket. To know about this function, we first have to know the concept of a socket.

A socket is inbuilt software that controls the total network and is treated as an endpoint for sending and receiving packets(data) all over the network. It behaves like a communicator between two nodes on the same or different networks.

setsockopt() is a function in the socket programming(application program) to control this communication and allocate some buffer space, time control, and in the field of broadcasting.”

Characteristics of setsockopt() Function

1. header file: setsockopt() function is a predefined function which definition is written in a header file in c library called <sys/socket.h> header file.

2. syntax: int setsockopt(int socket, int level, int option_name, const void *option_value, socketlen_t option_len) ;

3. return: If setsockopt () function executes properly it returns zero. But unsuccessful execution of setsockopt() function returns 0. For this it returns an integer value.

Application of setsockopt() Function

setsockopt() function is API. Using this API, we can set our server or provide the option to our server to listen to the same part.

Example-1

Let us know about the implementation of the setsockopt () function in this programming example.

#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netinet/tcp.h>

int main ()
{
        int  x1, x2, x3, x4  ;
        if ( ( x1 = socket ( AF_INET , SOCK_STREAM , 0 ) ) < 0 )
        {
                perror ( " Checking the device : " ) ;
                exit ( 0 ) ;
        }
        x4 = sizeof ( x2 ) ;
        if ( getsockopt ( x1 , IPPROTO_TCP , TCP_MAXSEG , ( char* ) &x2 , &x4 ) < 0 )
        {
                perror ( " Error occurred due to the function failure : " ) ;
                exit ( 0 ) ;
        }
        printf ( " \n The probablistic value of x2 is : = %d " , x2 ) ;
        x3 = 12324 ;
        if ( setsockopt ( x1 , SOL_SOCKET , SO_SNDBUF , ( char* ) &x3 , sizeof ( x3 ) ) < 0 )
        {
                perror ( " The chances of failure to respond " ) ;
                exit ( 0 ) ;
        }
        x4 = sizeof ( x3 ) ;
        if ( getsockopt ( x1 , SOL_SOCKET , SO_SNDBUF , ( char* ) &x3 , &x4 ) < 0 )
        {
                perror ( " Function does not respond properly : " ) ;
                exit(0);
        }
        printf ( " \nThe  buffer value is = %d \n " , x3 ) ;
        return 0 ;
}

Output

Explanation

To execute this program, we have to include some header file in our source code like <sys/types.h>, <sys/socket.h>, <netinet.h/in.h> and <netinet/tcp.h>. At first, we will create a socket to call a standard function socket () and pass some standard parameters inside its parenthesis. Now we will call the getsockopt () function to take the updated value for a socket and store it inside a variable named x2. Now we will set the value to the socket by calling the setsockopt () function. After that, we will again check the value for the socket in the buffer using the getsockopt () function and print its value.

Example-2

Here we will see another example of the setsockpt () function.

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

int main ( void ) ;

int main ()
{
int x1 ;
int x2 ;
socklen_t x3 = sizeof( x2 ) ;

/* Create the functional definition */
if ( ( x1 = socket ( PF_INET , SOCK_STREAM , IPPROTO_TCP ) ) < 0 )
   {
perror( " Creating the device : " ) ;
      exit ( EXIT_FAILURE ) ;
   }

/* Checking the condition of the functional device */
if ( getsockopt ( x1, SOL_SOCKET , SO_KEEPALIVE , &x2 , &x3 ) < 0 )
   {
perror( " Function may not respond properly : " ) ;
close ( x1 ) ;
      exit ( EXIT_FAILURE ) ;
   }
printf( " The state of the responding function is : %s \n " , ( x2 ? " ON " : " OFF " ) ) ;

/* Activate the functional option */
x2 = 1 ;
x3 = sizeof( x2 ) ;
if ( setsockopt ( x1, SOL_SOCKET , SO_KEEPALIVE , &x2 , x3 ) < 0 )
   {
perror( " Detecting some fault " ) ;
close ( x1 ) ;
      exit ( EXIT_FAILURE ) ;
   }
printf( " This functional value is installed  :\n " ) ;

   /* Check the status again */
if ( getsockopt ( x1, SOL_SOCKET , SO_KEEPALIVE , &x2 , &x3 ) < 0 )
   {
perror( " checking another time : " ) ;
close ( x1 ) ;
      exit ( EXIT_FAILURE ) ;
   }
printf( " Checking the ongoing response : %s \n " , ( x2 ? " ON " : " OFF " ) ) ;

close ( x1 ) ;

   exit ( EXIT_SUCCESS) ;
return 0 ;
}

Output

Explanation

In this programming example, we will first create a socket with the help of the socket () function and pass some arguments inside its parenthesis to make the socket. After that, we will call the getsockopt() function to check the socket states. Now we will call the setsockopt () function to set the state in active mode. Now we will again check the state of the socket to run the getsockopt () function and print the current state of the socket using “ON” or “OFF” mode. Actually, with the help of this program, we want to understand whether the keepalive state of the socket is active or not.

Conclusion

From the characteristics, implementation, and programming example of the setsockopt () function, it is clear to us very well that this function is really helpful in the case of the socket at the different network levels. With the help of the setsockopt () function, we can do different things like manipulating the length of sending and receiving buffer, their timeouts, etc. It provides us with the facility of multicasting also.

About the author

Bamdeb Ghosh

Bamdeb Ghosh is having hands-on experience in Wireless networking domain.He's an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python. Follow his site: wifisharks.com