Get Public/External Ip Address

C - Get External IP Address

"External IP" is a property of the network; not a property of the computer that is connected to the network. Thus, there is no function that you could call to get such information from the standard library or the operating system, as the system doesn't know the information.

I wrote a sample to get external IP address.

You wrote a program that gets the IP address of network interfaces. If the interface is connected to a public network, then the IP is external. If the interface is connected to a private network, then the IP is internal.


A solution to get the external IP through a private network is to connect to an external service that can see the IP from which the requests comes from. You seem to already know of this.

A more advanced approach that doesn't require external connections is to run similar service on the router system. As pointed out in a comment, UPnP or more specifically, IGD is such service that a router might provide. There is no standard UPnP client provided by C++ standard nor POSIX.

Getting a machine's external IP address with Python

If you are behind a router which obtains the external IP, I'm afraid you have no other option but to use external service like you do. If the router itself has some query interface, you can use it, but the solution will be very environment-specific and unreliable.

Getting the internet ip without calling an external server

You will probably not be able to grab your public IP Address without making a request to an external server, as your device is inside a LAN, it doesn't care about the public IP address of the router to Internet because it doesn't need it !

I would suggest you to use web-services like http://checkip.amazonaws.com/ to meet your needs.

    URL getIP = new URL("http://checkip.amazonaws.com/");
BufferedReader getIPReader = new BufferedReader(new InputStreamReader(getIP.openStream()));

System.out.println(getIPReader.readLine()); // prints the IP

How to get external IP Address?

If you are using a socket you can do socket.getInetAddress().getHostAddress()

If you want the device you get it's own public IP address, you can use this method:

URL whatismyip = new URL("http://checkip.amazonaws.com");
BufferedReader in = new BufferedReader(new InputStreamReader(whatismyip.openStream()));
String ip = in.readLine(); //IP as a string
System.out.println(ip);

ASP.NET: get external IP address

You can use it to get External (public) IP Address..

public static string getExternalIp()
{
try
{
string externalIP;
externalIP = (new WebClient()).DownloadString("http://checkip.dyndns.org/");
externalIP = (new Regex(@"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"))
.Matches(externalIP)[0].ToString();
return externalIP;
}
catch { return null; }
}


Related Topics



Leave a reply



Submit