Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting proper host IP  RSS feed

 
Jeff Holmes
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any ideas to the following would be great. Thought some one in this Forum might have insight. Thanks, Jeff
I'm using Linux and in my server procedure I want to determine 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
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 ServerSocket, calling accept on it to bring it to life, then calling its getInetAddress method? I haven't tried it, but it sounds like it might work.
jply
Jeff Holmes
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
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not positive, but you can try the following:
String hostName = InetAddress.getLocalHost().getHostName());
InetAddress inetAddresses[] = InetAddress.getAllByName( hostName );
This should return the array of all the IP address associated with your machine.
Let me know how it works
 
Jeff Holmes
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the info Carl. I tried that code on my machine and all I got was localhost.localdoamin/127.0.0.1 in the array. The ifconfig provides eth0 has inet addr 10.102.31.201 and that the l0 Local loopback is 127.0.0.1. Is there something I am missing on the Linux network setup side? Or is the problem actually with the Java side of things? I appreciate all the help, Thanks, Jeff
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps the answer is here http://developer.java.sun.com/developer/bugParade/bugs/4026796.html
However my Linux and Unix isn't what it should be.
Hope this helps
 
Jeff Holmes
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I take a look at this and start digging to get the solution. :ccol:
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!