29

I'm making a peer-to-peer instant messaging application.

Currently, if UserA.pool.net says "hello" to UserB.pool.net, User A sees "You: hello" and User B sees "UserA.pool.net: hello".

Rather than User A seeing "You", I want them to see the hostname of their own machine so that User A sees the same text as User B.

4

4 Answers 4

85

See these functions of java.net.InetAddress - getLocalHost and getHostName :

String localhostname = java.net.InetAddress.getLocalHost().getHostName();

Note that this gets you the hostname as the machine sees itself; others may see it with a different one (e.g. the local hosts file says something different than DNS). In other words, it is not guaranteed that machine A will be seen with the same hostname from machine A, machine B, or machine C.

As @biniam_Ethiopia points out, it is not even guaranteed that you'll get the same result from different programs on the same machine, as they may be using network-based name resolution (seen e.g. here).

It may be more useful to send the whole identifier: [email protected] , instead of just piskvor.

5
  • 1
    This is less than ideal under certain conditions. In particular InetAddress.getLocalHost() will throw an exception if the current hostname is set but unrecognized by the local dns server. Annoyingly there is no workaround available without using reflection to scrape the data out of the internals of InetAddress. Commented Jun 15, 2013 at 10:01
  • What if the host has multiple NICs and thus multiple names and IP addresses? What name will be returned??
    – Lawrence
    Commented Sep 17, 2013 at 13:09
  • @Lawrence: I don't think there's a defined order to this - you might get any one of them. Commented Sep 20, 2013 at 20:32
  • 1
    This method has a hole/problem. stackoverflow.com/questions/11143880/…
    – biniam
    Commented Nov 18, 2015 at 13:54
  • 1
    @biniam_Ethiopia: Thanks for the heads-up; that's pretty much the point of my second paragraph. "InetAddress.getHostName() is doing a reverse lookup on the server's IP address using the naming service (DNS) configured in your O/S" is a specific way of saying "it is not guaranteed that machine A will be seen with the same hostname from machine A, machine B, or machine C." But you are right, this is not obvious from the answer; edited to clarify. Commented Nov 18, 2015 at 15:03
15

The short answer is that if you really want User A and User B to see the same text, you can't rely on finding out your hostname yourself. You need User B to transmit their view of User A's hostname to User A and vice versa. Due to NAT, you won't be able to just check your own computer's hostname.

Alternatively, (Jonathon beat me to this in the question comments) you can have each user send their own private hostname as part of the connection handshake and use that to print messages on the remote end.

14

I've gotten the hostname of the local machine in the past using something like this:

InetAddress addr = InetAddress.getLocalHost();

String hostname = addr.getHostName();

You could refer to: InetAddress.getHostName()

5

You may need to use getCanonicalHostName() to get full qualified host name which include domain name also.

Code - String fullHostName = java.net.InetAddress.getLocalHost().getCanonicalHostName();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.