[java] java socket question

Started by
8 comments, last by _nomad_ 20 years ago
Hi, when i use this:

import java.net.*;

public class GetAddress {
    public static void main(String argv[]) throws Exception {
        InetAddress host = null;
        host = InetAddress.getInetAddress();
        byte ip[] = host.getAddress();

        for (int i=0; i 0) System.out.print(".");
           System.out.print(ip & 0xff);
        }
    }
}
 
i get ip address 192.168.XX.XX...but this isn''t the ip address i wish to get...i wish to get the same ip i get when i go to http://www.whatismyip.com, which is 210.23.XXX.XXX. how can i get the ip-address of the network which is 210.23.XXX.XXX in java socket? Thanks!
Advertisement
WORKING code would be much more useful

Anyway, I corrected it with getLocalHost() function - getInetAddress() does not seem to exist - and it worked fine : it returns my local address if I am disconnected and my net address when I am online. So make sure you''re connected when you run it.

import java.net.*;public class GetAddress{    public static void main(String argv[]) throws Exception    {        InetAddress host = InetAddress.getLocalHost();        byte ip[] = host.getAddress();        for(int i = 0; i < ip.length; i++)        {            if(i > 0) System.out.print(".");            System.out.print(ip[i] & 0xff);        }    }}
sorry, that should be getLocalHost(), i was typing that without compiling first.

anyway, if i use getLocalHost(), i get the problem stated on my first post.

thanks.
It fails even when you are connected ?

That''s weird, since the code I posted works perfectly on my machine. I can''t help you more...
it does not ''fail''...it''s just giving me the 192.168.x.x ip-address instead of the 210.23.x.x

anyone here knows how to get the 210.23.x.x (the one given by whatismyip.com)?

thanks.
The 192.168.nnn.nnn address is, of course, your IP on your LAN. I am by no means an expert on these matters, but I would not be surprised to find that there is no simple means of doing this: You may have to query a website of some sort, or perhaps querying your modem/router (which may be possible; look up its documentation).
If you know the ISP''s DHCP server IP adress you can probably work it out via that.
InetAddress.getLocalHost() will obviously return the address of the localhost, which is your LAN address.

When you have multiple addresses you must use NetworkInterface (JDK 1.4) to choose one of them.

import java.net.*;import java.util.Enumeration;public class GetAllAddresses{	public static void main(String[] argv) throws Exception	{		Enumeration interfaces = NetworkInterface.getNetworkInterfaces();		while(interfaces.hasMoreElements())		{			NetworkInterface i = (NetworkInterface) interfaces.nextElement();			Enumeration addresses = i.getInetAddresses();			System.out.print(i.getName() + " : ");			while(addresses.hasMoreElements())			{				InetAddress a = (InetAddress) addresses.nextElement();				System.out.print(a.getHostName() + " " + a.getHostAddress());			}			System.out.println();		}	}} 


[edited by - overnhet on March 27, 2004 1:49:23 PM]
yeah, it''s the IP on the LAN.

i tried running the NetworkInterface by overnhet, but it returns only the localhost and the IP on the LAN (the 192.X.X.X)...not the ip-address i get at whatismyip.com (which is 210.X.X.X)...

the code should run anywhere, so i don''t think knowing an ISP''s DHCP will work either...

thanks for the help so far guys, but am still stuck...
That''s networking for you. What you know is the IP address of the computer - which happens to be a private IP on a LAN. What you *want* to know is the external IP address of the gateway on that LAN. This is a non-trivial question - there''s nothing in Java that will help you with that. Please note that this is not Java''s fault; C++, C#, BASIC etc will all have the same problem.

If you *really*, *really* want to know the external IP, I suggest automatically performing a hit on What''s My IP (or a similar service) and parsing the result. However, I suspect this won''t help you!

The fact that there is a difference between your internal and external IP addresses means that there is some extra routing going on. I don''t know why you want to know the external IP address of the LAN, but if you''re attempting to pass it to someone else and to have them contact you via a socket I have to warn you that that probably won''t work. So their request hits the external IP - probably a hardware router of some kind. What now? Where does the router pass the packets? See the problem?

Tunneling through to a machine on an internal network from the outside world requires holes being opened in firewalls, packet routing rules being set up etc. This is all outside of your control as an application programmer.

For more information, look up NAT (Network Address Translation) to see how internal networks work, and research Passive vs Active FTP for an example of the problems of opening connections to a machine that''s behind a firewall/router. Look up private address ranges to find out what IPs are likely to be unreachable (192.168/16 etc).

You might want to consider a callback system from the client to a server - when the client initiates the connection the server can then communicate using that same connection, if the firewall allows it.

--cfmdobbie

This topic is closed to new replies.

Advertisement