Setsockopt Function In C

setsockopt is a function within the socket programming domain that manages communication by assigning buffer space, regulating timing, and sending data.

Characteristics of setsocket function

There are several characteristics of setsocket function . Some Characteristics of setsocket function are as follows:

  1. header file: The setsockopt method is a predefined function whose syntax is written in a header file in the c library named <sys/socket.h> .
  2. syntax:
  3. Example
    
    int setsockopt(int socket, int level, int option_name, const void *option_value, socketlen_t option_len) ;
    

When the setsockopt function is invoked successfully, it will return a value of zero. Conversely, if the setsockopt function call fails, it will return a non-zero integer value. This integer value is the indication of the failure in setting the socket options.

Applications of set setsockpt

The setsockopt function enables a software application to modify socket functionality. It provides the ability to adjust buffer sizes, manage timeouts, and enable broadcasting of socket data. The socket configuration options that can be modified using setsockopt are specified in the <sys/socket.h> header file.

Options could potentially be accessible across multiple protocol layers. The SO_ alternatives are consistently present at the utmost socket tier.

Example:

Filename: Socket.c

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  a1, a2, a3, a4  ;

        if ( ( a1 = socket ( AF_INET , SOCK_STREAM , 0 ) ) < 0 )

        {

                perror ( " The device checking : " ) ;

                exit ( 0 ) ;

        }

        a4 = sizeof ( a2 ) ;

        if ( getsockopt ( a1 , IPPROTO_TCP , TCP_MAXSEG , ( char* ) &a2 , &a4 ) < 0 )

        {

                perror ( " Error due to the function failures: " ) ;

                exit ( 0 ) ;

        }

        printf ( " \n The probablistic value of x2 is : = %d " , a2 ) ;

        a3 = 12323 ;

        if ( setsockopt ( a1 , SOL_SOCKET , SO_SNDBUF , ( char* ) &a3 , sizeof ( a3 ) ) < 0 )

        {

                perror ( " The total number chances of failure to respond " ) ;

                exit ( 0 ) ;

        }

        a4 = sizeof ( a3 ) ;

        if ( getsockopt ( a1 , SOL_SOCKET , SO_SNDBUF , ( char* ) &a3 , &a4 ) < 0 )

        {

                perror ( " The function does not responded properly: " ) ;

                exit(0);

        }

        printf ( " \nThe  buffered value is = %d \n " , a3 ) ;

        return 0 ;

}

Output:

Output

The probabilistic value of x2 is : = 536  

The  buffered value is = 24646

Explanation:

In this example, we must include the following header files in our source code: sys/types.h>, sys/socket.h>, netinet.h/in.h> , and netinet/tcp.h> to run this program. To begin, we will establish a socket that will be used to call a standard function socket and supply some standard arguments inside its parentheses. Now, we'll use the getsockopt method to get the most recent value for a socket and save it in a variable called a2 . Now, we'll use the setsockopt method to set the value of the socket. Following that, we will use the getsockopt method to check the value of the socket in the buffer once more and report its value.

Example: 2

Filename: Socket2.c

Example

#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 a1 ;

int a2 ;

socklen_t a3 = sizeof( a2 ) ;



// creating the function

if ( ( a1 = socket ( PF_INET , SOCK_STREAM , IPPROTO_TCP ) ) < 0 )

   {

perror( " Creating the device : " ) ;

      exit ( EXIT_FAILURE ) ;

   }



//The condition is validated

if ( getsockopt ( a1, SOL_SOCKET , SO_KEEPALIVE , &a2 , &a3 ) < 0 )

   {

perror( " The function does not responded properly : " ) ;

close ( a1 ) ;

      exit ( EXIT_FAILURE ) ;

   }

printf( " The present state of the responding function is : %s \n " , ( a2 ? " ON " : " OFF " ) ) ;



a2 = 1 ;

a3 = sizeof( a2 ) ;

if ( setsockopt ( a1, SOL_SOCKET , SO_KEEPALIVE , &a2 , a3 ) < 0 )

   {

perror( " the function has found the same fault " ) ;

close ( a1 ) ;

      exit ( EXIT_FAILURE ) ;

   }

printf( " This functional value has been is installed  :\n " ) ;



   // checking the functional status

if ( getsockopt ( a1, SOL_SOCKET , SO_KEEPALIVE , &a2 , &a3 ) < 0 )

   {

perror( " checking for another time : " ) ;

close ( a1 ) ;

      exit ( EXIT_FAILURE ) ;

   }

printf( " Monitoring the continuous response : %s \n " , ( a2 ? " ON " : " OFF " ) ) ;



close ( a1 ) ;



   exit ( EXIT_SUCCESS) ;

return 0 ;

}

Output:

Output

The present state of the responding function is:  OFF  

This functional value has been is installed  :

Monitoring the continuous response:  ON

Input Required

This code uses input(). Please provide values below: