File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Sockets and Internet Protocols and the fly likes Determining Servers IP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Determining Servers IP" Watch "Determining Servers IP" New topic
Author

Determining Servers IP

Jeff Holmes
Ranch Hand

Joined: Oct 04, 2000
Posts: 44
I'm using Linux and in my server procedure I want to dtermine the IP address so I can bind my Objects accordingly. I have been using the follwoing methods in the server:
InetAddress.getLocalHost().getHostAddress());
InetAddress.getLocalHost().getHostName());
byte[] test1 = InetAddress.getLocalHost().getAddress();
In windows these work just fine and return the proper IP, but in Linux I get an IP of 127.0.0.1. I can't seem to get the proper IP from the eth0. Any ideas on how to do this in the java.net? Or am I using the wrong thing? Thanks - Jeff
Jerry Pulley
Ranch Hand

Joined: Sep 19, 2000
Posts: 221
Jeff,
Seems like the Linux JDK interpretation of the "local host" is just that - "localhost", i.e. the loopback address 127.0.0.1. Have you tried instantiating a <code>ServerSocket</code>, calling <code>accept</code> on it to bring it to life, then calling its <code>getInetAddress</code> method? I haven't tried it, but it sounds like it might work.
jply

Jeff Holmes
Ranch Hand

Joined: Oct 04, 2000
Posts: 44
Jerry, sounds like a great idea. I implemented the idea and found out that the accept() method blocks untill a connection is made on that port. This doesn't help unfortunetly but I hit that port just to unblock the condition so I could get the code to continue. When it did continue I used the getInetAddress() on the socket that is created from the accept() method and then performed the getHostAddress() in the InetAddress object. But it returned 127.0.0.1. This is really confusing to me, but then again networking never was my forte. Thanks for the thought and anything else would be useful. Thanks - Jeff
Jerry Pulley
Ranch Hand

Joined: Sep 19, 2000
Posts: 221
Jeff,
OK, neither of us knows the answer, so I'm moving this to Sockets and Internet Protocols.
jply

[This message has been edited by Jerry Pulley (edited November 16, 2000).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Determining Servers IP