• 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

How to find the TCP/IP address of the current computer

 
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to find the currently active TCP/IP address (dotted Quad for IPv4) from a Java program.

I don't see how to start finding it. Sure, you can always use localhost, but I want to actual assigned address, from the DHCP server. From that, I can query the DNS resolver and find the name.

Everything I've found starts with either the name or the numbers.

Clearly this can be done, as all of the J2EE/JEE5 containers can provide it, as something has to tell the container what the host is to have at the start of the container's Context

From that, its provided in HttpServletRequet's getRequestURL() function. But in my testing, its always "localhost" and I want the real server name.

Thanks
Pat
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java.net.InetAddress?
 
Pat Farrell
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:java.net.InetAddress?


That's one class that I've crawled through, not seeing one.

Perhaps the static InetAddress.getLocalHost() may work, but I really an not interested in either "localhost" or 127.0.0.1
 
Pat Farrell
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting, it returns the localhost numeric values, 127.0.0.1, but also returns the host's name. Not the fully qualified name.
So on my machine named "tools" it returns tools. If it returned tools.pfarrell.com, then I could do a DNS lookup to get the number.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To clarify: are you looking for the IP address of just one of your network interfaces? Or would you be wanting all of them?
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java.net.NetworkInterface?
 
Pat Farrell
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:To clarify: are you looking for the IP address of just one of your network interfaces? Or would you be wanting all of them?



I'll take anything. Most times, there is only one NIC, so I want that IP address. If there are many, a List<> would be fine too
 
Pat Farrell
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:java.net.NetworkInterface?


Getting closer. with Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
I can walk through the list.

This is progress

Thanks
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
+ getInetAddresses, maybe.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
InetAddress works just fine for me:
Anonymized output:
pcname
pcname.domain.com
192.168.x.x
 
Pat Farrell
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting, I copied and pasted @rob's code into my application and it returned

tools
tools
127.0.1.1

Which means it did not pick up the doman in the getCanonicalHostName());
and looked up the value of localhost rather than the real address.

Wonder what is difference? I'm running this as a WebApp under Glassfish, started by Netbeans. But I did not expect that to make a difference.
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's probably in the network settings. I'm using Windows XP machine connected to a Windows domain at work and a Windows XP machine that is part of a workgroup at home.
 
Pat Farrell
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:It's probably in the network settings.


Or the OS. This is on Ubuntu 9.05
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then it's still in the network settings. You can get much different network settings than a completely different OS
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm interesting.

I tried it on my Ubuntu 9.10
Got
MyUbuntu
127.0.1.1
127.0.1.1

But, on my Mac got
MyMac.local
192.168.1.100
192.168.1.100

Both these machines are on the same wireless network at home (DHCP enabled). ifconfig on Ubuntu gives me the IP as 102.168.1.101
 
reply
    Bookmark Topic Watch Topic
  • New Topic