Getting the 'External' Ip Address in Java

Getting the 'external' IP address in Java

I am not sure if you can grab that IP from code that runs on the local machine.

You can however build code that runs on a website, say in JSP, and then use something that returns the IP of where the request came from:

request.getRemoteAddr()

Or simply use already-existing services that do this, then parse the answer from the service to find out the IP.

Use a webservice like AWS and others

import java.net.*;
import java.io.*;

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

String ip = in.readLine(); //you get the IP as a String
System.out.println(ip);

How to get external IP successfully

Before you run the following code take a look at this: http://www.whatismyip.com/faq/automation.asp

public static void main(String[] args) throws Exception {

URL whatismyip = new URL("http://automation.whatismyip.com/n09230945.asp");
URLConnection connection = whatismyip.openConnection();
connection.addRequestProperty("Protocol", "Http/1.1");
connection.addRequestProperty("Connection", "keep-alive");
connection.addRequestProperty("Keep-Alive", "1000");
connection.addRequestProperty("User-Agent", "Web-Agent");

BufferedReader in =
new BufferedReader(new InputStreamReader(connection.getInputStream()));

String ip = in.readLine(); //you get the IP as a String
System.out.println(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);

External IP Address of the client

If your client is using NAT (network address translation) it may not have an external address. Most often, in my experience, this is the case. At work, my web requests go through a proxy so the web server can only determine this address. At home I use NAT via a server so this laptop I'm typing on has no external address. The closest thing is what is returned from 'whatismyip', my server address, through which I may sometimes forward ports that go to my laptop.

Make a connection using an external IP address in Java

What you trying to do is a bit weird but it is possible.

You just need to know if your router supports NAT Loopback / NAT Reflection and activate it.

NAT-loopback

Also known as NAT-hairpinning or NAT-reflection. NAT-loopback is a
feature in many consumer routers which allows a user to connect to
its own public IP address from inside the LAN-network. This is
especially useful when a website (with domain) is hosted at that IP
address.

Java: Getting one's external IP address programmatically without using web services

The answer is a little complicated because it depends exactly how your computer is connected to the internet. If the computer is directly allocated a publicly routeable IP address then you can just list the network addresses assigned to each of the machine's network interfaces. For servers, this scenario is typical.

However, the computer may also be connected through a Network Address Translation (NAT) layer. In this case, your computer is not directly assigned a publicly routeable IP address; instead it is given a local IP address (such as 10.x.x.x or 192.168.x.x) and then a router (usually the next hop) will map packets from the local address space to the public address space. These setups can vary wildly, so it is recommended to just use a well known external service (like checkip.amazonaws.com which you mention), because that method should work regardless of the intermediate address translation.

Java socket: Get clients external IP

When you start to listen with serverSocket.accept(); and you store the Socket returning object of this function, you can get the client ip with this function from the socket with this: socket.getInetAddress().getHostAddress();.

Getting external IP address with Java

Assuming you are in a typical home enviroment (computer - router - internet) using NAT you cannot get that address (something like 83.154.48.7) from a program running in a local machine. The most you can do is get the address of the network adapters installed on it (addresses like 192.0.0.1), since the 'external' address is actually the address of the router.

You need to ask a external site because they will answer to the address of the router (the 83.154.48.7) and the router will send the answer to your pc.

A posibility could be to ask the router for his IP, but viewed from the PC, the router will have an address like 192.0.0.255 for that network. (Dont know if you can get ALL addresses in the router and filter them, though)

For if one day this site down, I will have big problems.

You can have a list of sites where you can get the address from, and try one by one until one of them is up.



Related Topics



Leave a reply



Submit