Reputation: 1403
How do i get the Wide Area Network of my computer with Java? I try with this:
ServerSocket ss = new ServerSocket(port);
System.out.println(ss.getInetAddress().getHostAddress());
//wich return 0.0.0.0
then i try with this:
System.out.println(InetAddress.getLocalHost().toString());
//which return keenan-a658368c/192.168.1.100 < yes it is connected to router
like the function said, it return my local IP address
How do i get the WAN IP Address? such as 118.137.43.219
Upvotes: 1
Views: 7765
Reputation: 13614
To get the IP address of the computer's primary interface:
InetAddress.getLocalHost().getHostAddress()
To get the IP addresses of all interfaces:
List<InetAddress> addresses = new LinkedList<InetAddress>();
Enumeration<NetworkInterface> ifcs = NetworkInterface.getNetworkInterfaces();
while (ifcs.hasMoreElements()) {
NetworkInterface ifc = ifcs.nextElement();
for (InterfaceAddress ifcAddr : ifc.getInterfaceAddresses()) {
addresses.add(ifcAddr.getAddress());
}
}
To get the IP address that other machines on the Internet would see connections from your computer as coming from, go with YoK's answer.
Upvotes: 0
Reputation: 4882
If you end up using a remote service that replies back with your "external IP address" (see other answers for a definition of what it may be), don't use one of the free no-name ones. Deploy your own. You must not build an application that depends on someone's Acme Whats-My-IP 3000 that may go away at any time and without notice to you, or any other unfortunate users.
Upvotes: 2
Reputation: 63538
In general any code which depends on doing this has a design fault - could you elaborate as to WHY you need to get the egress IP address of their NAT router?
The egress IP address will not assist you at all in creating a connection back, as the router will typically not forward it to the appropriate internal host.
Upvotes: -1
Reputation: 14505
You can get it from http://whatismyip.com/automation/n09230945.asp. You can open an HttpURLConnection to this site and parse output.
This Program should be helpful :
import java.net.HttpURLConnection;
public class GetExternalIp {
public static void main(String args[]) {
try {
java.net.URL url = new java.net.URL(
"http://whatismyip.com/automation/n09230945.asp");
java.net.HttpURLConnection con = (HttpURLConnection) url
.openConnection();
java.io.InputStream stream = con.getInputStream();
java.io.InputStreamReader reader = new java.io.InputStreamReader(
stream);
java.io.BufferedReader bReader = new java.io.BufferedReader(reader);
System.out.print("Your IP address is " + bReader.readLine());
} catch (Exception e) {
e.printStackTrace();
}
}
}
Referenced from :
http://www.daniweb.com/forums/thread192872.html
http://www.coderanch.com/t/411356/java/java/Public-IP-Address-time-limit
Upvotes: 2
Reputation: 21618
As stated in comments, if you are behind a router that is performing NAT your machine will not know its WAN address.
A more complicated case is where you are behind a NAT pool. If this is true then your WAN address might change periodically, perhaps once a day or more often.
Or certain types of traffic might be forced through a proxy. This could make outbound HTTP requests come from a different WAN address than SSH or other arbitrary protocols.
Upvotes: 1