Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
241 views
in Technique[技术] by (71.8m points)

java - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...