CSocketWrench::GetPeerAddress Method  
 
INT GetPeerAddress(
  LPINTERNET_ADDRESS lpAddress,  
  UINT *lpnRemotePort  
);
INT GetPeerAddress(
  LPTSTR lpszAddress,  
  INT nMaxLength  
);
INT GetPeerAddress(
  CString& strAddress  
);

The GetPeerAddress method returns the peer IP address and remote port number for the specified socket.

Parameters

lpAddress
A pointer to an INTERNET_ADDRESS structure that will contain the IP address of the remote host that is connected to the socket.
lpnRemotePort
A pointer to an unsigned integer that will contain the port number of the remote host that is connected to the socket.
lpszAddress
A pointer to a string buffer that will contain the formatted IP address, terminated with a null character. To accommodate both IPv4 and IPv6 addresses, this buffer should be at least 46 characters in length.
nMaxLength
The maximum number of characters that can be copied into the address buffer.

Return Value

If the method succeeds, the return value is zero. If the method fails, the return value is INET_ERROR. To get extended error information, call GetLastError.

Remarks

If this method is called by a server application in response to a INET_EVENT_ACCEPT event, it will return the IP address and port number for the client that is attempting to establish the connection. If the peer address is unavailable, the ipFamily member of the INTERNET_ADDRESS structure will be zero.

The format and length of IPv4 and IPv6 address strings are very different. An IPv4 address string looks like "192.168.0.20", while an IPv6 address string can look something like "fd7c:2f6a:4f4f:ba34::a32". If your application checks for the format of these address strings, it needs to be aware of the differences. You also need to make sure that you're providing enough space to display or store an address to avoid buffer overruns.

It is not recommended that you use the port number for anything other than informational and logging purposes. Server applications should not make any assumptions about the specific port number or range of port numbers that a client is using when establishing a connection to the server. The ephemeral port number that a client is bound to can vary based on the client operating system.

Requirements

Minimum Desktop Platform: Windows 7 Service Pack 1
Minimum Server Platform: Windows Server 2008 R2 Service Pack 1
Header File: cswsock11.h
Import Library: cswskv11.lib

See Also

GetHostAddress, GetHostName, GetLocalAddress, GetLocalName, GetPeerPort, INTERNET_ADDRESS