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!
🔒 Locked
[java] java socket question
Advertisement
Original Post
Hi,
when i use this:
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.
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);
}
}
}
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.
[edited by - overnhet on March 27, 2004 1:49:23 PM]
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...
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.
Advertisement