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.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Matt
  • 11,157
  • 26
  • 81
  • 110
  • 3
    possible duplicate of [Java current machine name and logged in user?](http://stackoverflow.com/questions/473446/java-current-machine-name-and-logged-in-user) – Jonathon Faust Apr 08 '11 at 14:48
  • It's a bit more complicated than that Jonathon...if you're behind a NAT layer User A and User B will definitely not see the same thing. – Mark Peters Apr 08 '11 at 14:50
  • 2
    You're right, each participant will need to report their own (internal) host name to the other party. – Jonathon Faust Apr 08 '11 at 14:51
  • See also http://stackoverflow.com/questions/6050011/how-do-i-get-the-local-hostname-if-unresolvable-through-dns-in-java – Raedwald Aug 22 '13 at 15:08

4 Answers4

84

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: piskvor@lachtan.my.network.example.com , instead of just piskvor.

Community
  • 1
  • 1
Piskvor left the building
  • 91,498
  • 46
  • 177
  • 222
  • 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. – Kevin Sitze Jun 15 '13 at 10:01
  • What if the host has multiple NICs and thus multiple names and IP addresses? What name will be returned?? – Lawrence Sep 17 '13 at 13:09
  • @Lawrence: I don't think there's a defined order to this - you might get any one of them. – Piskvor left the building Sep 20 '13 at 20:32
  • 1
    This method has a hole/problem. http://stackoverflow.com/questions/11143880/why-does-inetaddress-getlocalhost-gethostname-return-a-value-different-from – biniam Nov 18 '15 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. – Piskvor left the building Nov 18 '15 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.

Mark Peters
  • 80,126
  • 17
  • 159
  • 190
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()

Diego
  • 16,830
  • 8
  • 34
  • 46
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();

Viraj Kulkarni
  • 129
  • 2
  • 4