Skip to main content
gamedev.net
🔒 Locked

[java] java socket question

Started by _nomad_ Mar 24, 2004 at 8:19 AM 8 replies 850+ views
Original Post
_nomad_
_nomad_
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!
overnhet
overnhet
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);
}
}
}
_nomad_
_nomad_
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.
overnhet
overnhet
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...
_nomad_
_nomad_
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.
Miserable
Miserable
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).
PurpleAmethyst
PurpleAmethyst
If you know the ISP''s DHCP server IP adress you can probably work it out via that.
overnhet
overnhet
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]
_nomad_
_nomad_
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...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.