Java reporting wrong IP address in SuSE 9.2

S

subscriber

I have a strange problem. When I try to determine my machine's IP
address from Java (ver 1.4.2_06), I get the wrong result. It reports
192.168.0.88 when the address is actually 192.168.0.60.
I am running SuSE 9.2.

Here's the code (which gives the output "algw-05/192.168.0.88"):

public class TestMain {
public static void main(String[] args) {
try {
String myHostName =
InetAddress.getLocalHost().getHostName();
InetAddress[] myAddresses =
InetAddress.getAllByName(myHostName);
for (int i = 0; i < myAddresses.length; i++) {
System.out.println(myAddresses.toString());
}
}
catch (UnknownHostException ignored) {
}
}
}

And here's the output from ifconfig:

ath0 Link encap:Ethernet HWaddr 00:05:4E:46:6F:7D
inet addr:192.168.0.60 Bcast:192.168.0.255
Mask:255.255.255.0
inet6 addr: fe80::205:4eff:fe46:6f7d/64 Scope:Link
UP BROADCAST NOTRAILERS RUNNING MULTICAST MTU:1500 Metric:1
RX packets:19954 errors:148 dropped:0 overruns:0 frame:148
TX packets:17833 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:199
RX bytes:18750042 (17.8 Mb) TX bytes:2639289 (2.5 Mb)
Interrupt:11 Memory:f9220000-f9230000

Any ideas?
// Ake
 
G

Gordon Beaton

I have a strange problem. When I try to determine my machine's IP
address from Java (ver 1.4.2_06), I get the wrong result. It reports
192.168.0.88 when the address is actually 192.168.0.60.
I am running SuSE 9.2.

It sounds to me like you need to check the contents of your /etc/hosts
or /etc/nsswitch.conf.

What hostname does this line report:
InetAddress.getLocalHost().getHostName();

And what does nsloookup <yourhostname> report?

What happens if you use
java.net.NetworkInterface.getNetworkInterfaces() to iterate over the
available interfaces, and then call getInetAddresses() in each?

/gordon
 
?

=?iso-8859-1?B?xWtlIEw=?=

Thanks for your help Gordon.
For some reason the /etc/hosts file contained the wrong IP address.

The code below (as you suggested) gave the correct IP of course:

Enumeration en = java.net.NetworkInterface.getNetworkInterfaces();
while (en.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface) en.nextElement();
System.out.println(ni);
}

// Ake
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top