setsockopt

Establishes parameters for socket operation.

Library:LibC
Standard:BSD
Service:Networking

Syntax

  #include <sys/socket.h> 
   
  int setsockopt (
     int          s,
     int          level,
     int          optname,
     const void  *optval,
     size_t       optlen);
  

Parameters

s

(IN) Specifies the socket file handle for which parameters are to be established.

level

(IN) Specifies the protocol level at which the option resides with one of the following flags:

Flag

Value

Description

IPPROTO_IP

0

Specifies IP level options

IPPROTO_TCP

6

Specifies TCP level options

SOL_SOCKET

0xFFFF

Specifies socket level options

optname

(IN) Specifies the option for the request. See one of the following:

optval

(IN) Points to the location for the option value.

optlen

(IN) Specifies the length of the option.

Return Values

If successful, returns 0. Otherwise, returns -1 and sets errno to one of the following:

Decimal

Constant

Description

4

EBADF

The socket parameter is not a valid socket file handle.

9

EINVAL

The optlen parameter is inappropriate.

38

ENOTSOCK

The socket parameter does not reference a socket.

42

ENOPROTOOPT

The option is unknown at the level indicated.

Remarks

Options exist at either the socket level or the underlying protocol level (see sys/ socket.h for socket-level option definitions). When setting socket options, you must specify the name of the option and the level at which it resides:

  • To set options at the socket level, specify level as SOL_SOCKET.

  • To set options at the protocol level, supply the number of the underlying protocol controlling the option.

For example, for IPv4 and IPv6 protocol families, if you want TCP to interpret an option, set the level to IPPROTO_TCP.