java.net.InetAddress

Michael Koch konqueror@gmx.de
Sat Jun 21 08:16:00 GMT 2003


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Am Samstag, 21. Juni 2003 01:55 schrieb Erik Poupaert:
> Hi
>
> When I execute 'ifconfig' on my machine, I get the following
> output:
>
> eth0      Link encap:Ethernet  HWaddr 00:0B:CD:54:AE:61
>           inet addr:192.168.0.2  Bcast:192.168.0.255
> Mask:255.255.255.0 ...
>
> lo        Link encap:Local Loopback
>           inet addr:127.0.0.1  Mask:255.0.0.0
> 	...
>
> On Windows, I get something similar with 'ipconfig'.
>
> What I'm actually interested in, is the address "192.168.0.2"; the
> one assigned to my machine by dhcpcd.
>
> Regardless of what I try, however, java.net.InetAddress will never
> return this address. It will always return the (absolutely useless)
> loopback address "127.0.0.1":
>
> String localHost=InetAddress.getLocalHost().getHostName();
> System.out.println("local ip: " +
> InetAddress.getLocalHost().getHostAddress());
> System.out.println("localhost: " + localHost);
> System.out.println("getByName:" +
> InetAddress.getByName(localHost).getHostAddress());
> InetAddress[] addresses=InetAddress.getAllByName(localHost);
> System.out.println("address count: " + addresses.length);
> for(int i=0;i<addresses.length;i++)
> {
> 	String ip=addresses[i].getHostAddress();
> 	System.out.println("ip:" + ip);
> }
>
> Does anybody know, how I can get hold of the dhcp-assigned or
> statically assigned address?

Exactly your code gives me the following for my personal computer:

local ip: 192.168.1.3
localhost: asterix.konqueror.de
getByName:192.168.1.3
address count: 1
ip:192.168.1.3

and with SUNs JDK 1.4.1_01:

local ip: 192.168.1.3
localhost: asterix
getByName:192.168.1.3
address count: 2
ip:192.168.1.3
ip:fec0:0:0:0:0:0:0:3

Well, IPv6 support is not ready in libgcj.

I really wonder why your test output 127.0.0.1. Can you send me your
/etc/hosts file ? I think its borked.


Michael

- --
Homepage: http://www.worldforge.org/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQE+9BUXWSOgCCdjSDsRAhIWAJ9RH9dOAjKD7yyGP4rxSppTCcn8ZgCeOM1q
62yccAdXvfXk5zu8PVglF/U=
=ZFjO
-----END PGP SIGNATURE-----



More information about the Java mailing list