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_ANY Return any external IP address available. IPv4 addresses will be preferred over IPv6 for backwards compatibility.
INET_ADDRESS_IPV4 Specifies that the external address should be in IPv4 format.
INET_ADDRESS_IPV6 Specifies that the external address should be in IPv6 format. If the local system does not have IPv6 enabled, the method will fail.
lpAddress
A pointer to an INTERNET_ADDRESS structure that will contain the external IP address of the local host in binary form.
lpszAddress
A null-terminated 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

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

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 method 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.

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 method may be unable to determine the external IP address for the local system for a number of reasons, particularly if it is behind a firewall or uses a proxy server that restricts access to external sites on the Internet. If the method is able to obtain a valid external address for the local host, that address will be cached by the library. If you are using a VPN (Virtual Private Network), this method will return the external IP address for the VPN adapter and not your local network connection. The GetVpnAdapter method can be used to obtain information about the active VPN adapter.

Calling this method 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 method in an application which uses asynchronous sockets, it is recommended that you create a background worker thread and call this method 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: cswsock12.h
Import Library: cswskv12.lib
Unicode: Implemented as Unicode and ANSI versions

See Also