The getsockopt function

Description

The getsockopt function retrieves the current value of a socket's option. If an option has not been set with the setsockopt function, then the getsockopt function returns the default value for the option.

Declaration

The system include file WinSock2.inc contains the following declaration for the getsockopt function:

 function getsockopt(s : SOCKET; level, optname : integer; optval : address; var optlen : integer) : integer;
  external dll='ws2_32.dll';

Arguments

The First Argument

The first argument passed to the getsockopt function is the socket from which you want to get the option value.

The Second Argument

The second argument passed to the getsockopt function is the level at which the socket option is defined. You will usually use SOL_SOCKET as the value of this argument.

The Third Argument

The third argument passed to the getsockopt function is an integer value that identifies the socket option whose value you want to retrieve. The socket options you are most likely to retrieve are given below:

NOTE: Socket options of type BOOL are retrieved into an integer, and a value of zero means false, and an non-zero value means true.

The Fourth Argument

The fourth argument passed to the getsockopt function is the operating system address of the buffer you have supplued to store the value of the socket option. You should use the built-in function addr to get the operating system address of this buffer. For the SO_LINGER socket option, the buffer should be a linger structure, for the other options the buffer should be of integer type.

The Fifth Argument

The fifth argument passed to the getsockopt function is the length of the buffer you supplied to store the value of the socket option. After the getsockopt function returns successfully this argument will contain the actual length of the socket option value stored in the buffer.

Return Values

The getsockopt function returns a value of integer type that indicates whether the call was successful. A value of zero means that the call succeeded. A value of SOCKET_ERROR indicates that the called failed, and in this case you can use the WSAGetLastError function to retrieve a code that identifies the error that caused the call to fail. NOTE: The constant SOCKET_ERROR is declared in the system include file WinSock2.inc.

Reference Information

The authoritative source of information about the WinSock2 library is the Microsoft Developers Network (MSDN). You can access the MSDN on the Microsoft website at msdn.microsoft.com.