tags:

views:

373

answers:

4

Hi experts

I am trying to get hostname/computer name using this method. Unfortunately i only can get localhost but not other computer.

private String getHostName(String _strIP) {
    try {
        InetAddress inetAddress = InetAddress.getByName(_strIP);
        System.out.println("getHostAddress : " + inetAddress.getHostAddress());
        System.out.println("getHostName : " + inetAddress.getHostName());
        System.out.println("getCanonicalHostName : " + inetAddress.getCanonicalHostName());
        return inetAddress.getHostName();            
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }
    return strDefaultHostName;
}

the result (not localhost)

getHostAddress : 192.168.2.139
getHostName : 192.168.2.139
getCanonicalHostName : 192.168.2.139

the result (localhost)

getHostAddress : 127.0.0.1
getHostName : localhost
getCanonicalHostName : localhost

Thank you

A: 

The javadoc of InetAddress.getCanonicalHostName() says:

Gets the fully qualified domain name for this IP address. Best effort method, meaning we may not be able to return the FQDN depending on the underlying system configuration.

If there is a security manager, this method first calls its checkConnect method with the hostname and -1 as its arguments to see if the calling code is allowed to know the hostname for this IP address, i.e., to connect to the host. If the operation is not allowed, it will return the textual representation of the IP address.

I looks like your system configuration isn't correct. Are you running from within an applet?

tangens
it's a private network based on the IP address. It's most likely that there is no DNS server to tell him what hostname corresponds to '192.168.2.139'. Most cheap and cheerful home routers don't do internal DNS, so it's not mapping back
Petesh
All PCs in my office are running XP-proCan i know where need to configure to allow this operation?Thanks
Joe Ijam
+2  A: 

We've established roughly what the problem is in tangens' answer.

I think you can fix the problem pretty simply by putting host names into your hosts file.

%SystemRoot%\system32\drivers\etc\hosts

is the file you're looking for; localhost is defined here. You want to put a name and address line in it for every host you want to resolve.

I've never tried this. If it doesn't work, you get your money back.


Update

The above is the "quick hack" solution. This essentially entails that whenever someone manually changes the IP address of a host you're interested in, someone must at the same time change the hosts files on any machines that want to access those hosts.

The other alternative is to operate your own DNS server. You still need to update IP addresses when a host's address changes, but you only need to do so in one place, and you get both forward and reverse name resolution throughout your network. This takes more setting up but is easier to maintain in the long run.

Here is a very useful reference: http://www.dns.net/dnsrd/servers/windows.html

They mention that the "built in" Microsoft DNS server is a terrible solution (up until the one in Windows 2003 Server) but mention at least two alternatives, one commercial and one free. BIND is what is currently holding much of the Internet together, DNS-wise, and it's great that they have a Windows port too.

Carl Smotricz
A: 

Reply Feedback for Carl Smotricz

Great answer, but we still don't know if the host name has been updated or not... This is something like we hardcode.

Anyway thank you so much

# Copyright (c) 1993-1999 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

127.0.0.1       localhost
192.168.2.139       dev-testing
Joe Ijam
Please see my updated answer.
Carl Smotricz
don't post this as an answer, just update the question by clicking edit please.
Jeff Atwood
A: 

Your DNS is broken. Then IP-numbers are returned instead.

Thorbjørn Ravn Andersen