CSocketWrench::GetLocalAddress Method  
 
INT GetLocalAddress(
  INT nAddressFamily,  
  LPINTERNET_ADDRESS lpAddress,  
  UINT *lpnPort  
);
INT GetLocalAddress(
  LPTSTR lpszAddress,  
  INT nMaxLength  
);
INT GetLocalAddress(
  CString& strAddress,  
  UINT *lpnPort  
);

The GetLocalAddress method returns the local IP address and port number for the current socket.

Parameters

nAddressFamily
An integer which identifies the type of IP address to return. It may be one of the following values:
Value Description
INET_ADDRESS_UNKNOWN Return the IP address for the specified host in either IPv4 or IPv6 format, depending on the type of connection that was established. If the hSocket parameter is INVALID_SOCKET, a preference will be given for returning an IPv4 address. However, if the local host only has an IPv6 address, that value will be returned.
INET_ADDRESS_IPV4 Specifies that the address should be returned in IPv4 format. The first four bytes of the ipNumber array are significant and contains the IP address. The remaining bytes are not significant and an application should not depend on them having any particular value, including zero.
INET_ADDRESS_IPV6 Specifies that the address should be returned in IPv6 format. All bytes in the ipNumber array are significant. Note that it is possible for an IPv6 address to actually represent an IPv4 address. This is indicated by the first 10 bytes of the address being zero.
lpAddress
A pointer to an INTERNET_ADDRESS structure that will contain the IP address of the local host. If there is no active connection, this function will attempt to determine the IP address of the local host assigned by the system. If the address is not required, this parameter may be NULL.
lpszAddress
A pointer to a null terminated string that will contain the IP address of the local host. If this version of the method is used, the IP address is converted to a string format using the FormatAddress method. The string should be able to store at least 46 characters to ensure that both IPv4 and IPv6 formatted addresses can be returned without the possibility of a buffer overrun. An alternate form of the method accepts a CString argument which will contain the local address.
lpnPort
A pointer to an unsigned integer that will contain the local port number. If there is an active connection, this parameter will be set to the local port that the socket is bound to. If there is no active connection, this parameter is ignored. If the local port number is not required, this parameter may be NULL.

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

To ensure future compatibility with IPv6 networks, it is important that the application does not make any assumptions about the format of the address. If the function returns successfully, the ipFamily member of the INTERNET_ADDRESS structure should always be checked to determine the type of address.

If the nAddressFamily parameter is specified as INET_ADDRESS_UNKNOWN, the application must be prepared to handle IPv6 addresses because it is possible for the local host to have an IPv6 address assigned to it and no IPv4 address. For legacy applications that only recognize IPv4 addresses, the nAddressFamily member should always be specified as INET_ADDRESS_IPV4 to ensure that only IPv4 addresses are returned.

If the system is connected to the Internet through a local network and/or uses a router that performs Network Address Translation (NAT), the GetLocalAddress method will return the local, non-routable IP address assigned to the local system. To determine the public IP address has been assigned to the system, you should use the GetExternalAddress method.

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
Unicode: Implemented as Unicode and ANSI versions

See Also

GetExternalAddress, GetHostAddress, GetHostName, GetLocalName, GetPeerAddress, INTERNET_ADDRESS