CSocketWrench::GetExternalAddress Method  
 
INT GetExternalAddress(
  INT nAddressFamily,  
  LPINTERNET_ADDRESS lpAddress,  
  INT nMaxLength  
);
INT GetExternalAddress(
  INT nAddressFamily,  
  LPTSTR lpszAddress,  
  INT nMaxLength  
);
INT GetExternalAddress(
  INT nAddressFamily,  
  CString& strAddress  
);

The GetExternalAddress method returns the external IP address for the local system.

Parameters

nAddressFamily
An integer which identifies the type of IP address that should be returned by this function. It may be one of the following values:
Value Description
INET_ADDRESS_IPV4 Specifies that the address should be in IPv4 format. The method will attempt to determine the external IP address using an IPv4 network connection.
INET_ADDRESS_IPV6 Specifies that the address should be in IPv6 format. The method will attempt to determine the external IP address using an IPv6 network connection and requires that the local host have an IPv6 network interface installed and enabled.
lpAddress
A pointer to an INTERNET_ADDRESS structure that will contain the external IP address of the local host in binary form.
lpszAddress
A pointer to a string buffer that will contain the external IP address of the local host.
nMaxLength
The maximum length of the string that will contain the IP address when the method returns.

Return Value

In the first form of the method, if it succeeds, the return value is the IP address of the local system in numeric form. If the method fails, the return value is INET_ADDRESS_NONE. In the second form, the return value is the length of the IP address string and an error is indicated by the return value INET_ERROR. To get extended error information, call GetLastError.

Remarks

The GetExternalAddress method returns the IP address assigned to the router that connects the local host to the Internet. This is typically used by an application executing on a system in a local network that uses a router which performs Network Address Translation (NAT). In that network configuration, the GetLocalAddress method will only return the IP address for the local system on the LAN side of the network unless a connection has already been established to a remote host. The GetExternalAddress function can be used to determine the IP address assigned to the router on the Internet side of the connection and can be particularly useful for servers running on a system behind a NAT router.

This method requires that you have an active connection to the Internet and calling this function on a system that uses dial-up networking may cause the operating system to automatically connect to the Internet service provider. An application should always check the return value in case there is an error; never assume that the return value is always a valid address. The function may be unable to determine the external IP address for the local host for a number of reasons, particularly if the system is behind a firewall or uses a proxy server that restricts access to external sites on the Internet. If the function is able to obtain a valid external address for the local host, that address will be cached by the library for sixty minutes. Because dial-up connections typically have different IP addresses assigned to them each time the system is connected to the Internet, it is recommended that this function only be used with broadband connections where a NAT router is being used.

Calling this function may cause the current thread to block until the external IP address can be resolved and should never be used in conjunction with asynchronous socket connections. If you need to call this function in an application which uses asynchronous sockets, it is recommended that you create a new thread and call this function from within that thread.

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

GetHostAddress, GetLocalAddress, GetPeerAddress