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.