aspose file tools*
The moose likes Java in General and the fly likes How to find the TCP/IP address of the current computer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to find the TCP/IP address of the current computer" Watch "How to find the TCP/IP address of the current computer" New topic
Author

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

Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

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

Joined: Sep 29, 2008
Posts: 12617

java.net.InetAddress?
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

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

Joined: Aug 11, 2007
Posts: 4659
    
    5

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
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

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

Joined: Sep 29, 2008
Posts: 12617

java.net.NetworkInterface?
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

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

Joined: Aug 11, 2007
Posts: 4659
    
    5

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

Joined: Sep 29, 2008
Posts: 12617

+ getInetAddresses, maybe.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19790
    
  20

InetAddress works just fine for me:
Anonymized output:
pcname
pcname.domain.com
192.168.x.x


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

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

Joined: Oct 27, 2005
Posts: 19790
    
  20

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

Joined: Aug 11, 2007
Posts: 4659
    
    5

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

Or the OS. This is on Ubuntu 9.05
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19790
    
  20

Then it's still in the network settings. You can get much different network settings than a completely different OS
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10535
    
    9

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


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to find the TCP/IP address of the current computer