Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get DNS name of local machine as seen by a remote machine

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.

like image 459
Matt Avatar asked Apr 08 '11 14:04

Matt


People also ask

How do I find the hostname of a remote computer?

Querying DNS Click the Windows Start button, then "All Programs" and "Accessories." Right-click on "Command Prompt" and choose "Run as Administrator." Type "nslookup %ipaddress%" in the black box that appears on the screen, substituting %ipaddress% with the IP address for which you want to find the hostname.

How do I find the DNS name of an IP address?

If you're using a Windows computer, go to Start, then Run, and type command to open the command prompt. Type nslookup and hit Enter. Your search will bring back information about your local DNS default server and IP address.

How do I find the hostname of an IP address in Linux?

A far simpler and more common way to look up the hostname from an IP address is to use nslookup. Nslookup is a command-line utility, similar to dig, but that allows users to query DNS for hostnames and IP address mappings. 110.223.58.216.in-addr.arpa name = mba01s08-in-f14.1e100.net.


2 Answers

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.

like image 138
Piskvor left the building Avatar answered Oct 14 '22 07:10

Piskvor left the building


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.

like image 20
Mark Peters Avatar answered Oct 14 '22 07:10

Mark Peters