views:

98

answers:

5

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

+1  A: 

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.

Darron
+3  A: 

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

YoK
A: 

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.

MarkR
it is just for the user, so it will look like: making Server on xxx.xx.xxx.xxx on port 5555, and then you now that you can access this server (or ServerSocket object) on that ip
Keenan Gebze
+1  A: 

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.

Max A.
okaay, then i will rent a free webhost and then place the php script :D
Keenan Gebze
A free hosting service is about as bad as the no-name externally hosted PHP scripts mentioned above. My point is, don't develop any crucial business processes that rely on resources that can go away w/o notice.
Max A.
A: 

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.

Jonathan