• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Getting proper host IP

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, I take a look at this and start digging to get the solution. :ccol:
reply
    Bookmark Topic Watch Topic
  • New Topic