The getsockname function retrieves a socket's name, and can be useful in cases where you have not explicity assigned a name to the socket, and the system has assigned a name for you. One such case occurs, if you use the connection function on a socket without first assigning a name to the socket with the bind function.
For connection-oriented sockets, you should not assume that the socket's address will available, if the socket has been bound to an unspecified address (using ADDR_ANY), and the socket is not connected.
For connectionless sockets, the address may not be available unil I/O occurs on the socket.
The system include file WinSock2.inc contains the following declaration for the getsockname function:
function getsockname(s : SOCKET; name : address; var namelen : integer) : integer;
external dll='wsock32.dll';
The first argument passed to the getsockname function is the socket whose name you want to get.
The second argument passed to the getsockname function is the operating system address of a buffer you have supplied to store the name of the socket. You should use the built-in function addr to get the operating system address of this buffer.
The third argument passed to the getsockname function is the length of the buffer you supplied to store the name of the socket. After the getsockname function returns successfully this argument will contain the actual length of the name stored in the buffer.
The getsockname 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.
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.