The getpeername function

Description

The getpeername function retrieves the name of a socket's peer (i.e. the name of the socket at the other end of the connection). NOTE: In the case of a connectionless socket, the name retrieved will be the name of the default source or destination sockets, specified by a previous call to the connect function. The names of sockets specified in previous calls to the sendto function will not be retrieved by this function.

Declaration

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

 function getpeername(s : SOCKET; name : address; var namelen : integer) : integer;
  external dll='wsock32.dll';

Arguments

The First Argument

The first argument passed to the getpeername function is the socket whose peer you want to get the name of.

The Second Argument

The second argument passed to the getpeername function is the operating system address of a buffer to store the name of the socket at the other end of the connection. You should use the built-in function addr to get the operating system address of this buffer.

The Third Argument

The third argument passed to the getpeername function is the length of the buffer you supplied to store the name of the socket at the other end of the connection. After the getpeername function returns successfully this argument will contain the actual length of the name stored in the buffer.

Return Values

The getpeername 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.