Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • 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
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.net.InetAddress?
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • 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: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
Paul Clapham
Sheriff
Posts: 21318
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.net.NetworkInterface?
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • 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: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • 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
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
+ getInetAddresses, maybe.
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
 
Maneesh Godbole
Saloon Keeper
Posts: 11092
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic