tags:

views:

60

answers:

3

I want to find client computer name in java. My applciation runs in intranet. so i am using below code

   public String findClientComputerName(HttpServletRequest request) {
    String computerName = null;
    String remoteAddress = request.getRemoteAddr();
    System.out.println("remoteAddress: " + remoteAddress);
    try {
        InetAddress inetAddress = InetAddress.getByName(remoteAddress);
        System.out.println("inetAddress: " + inetAddress);
        computerName = inetAddress.getHostName();
        System.out.println("computerName: " + computerName);
        if (computerName.equalsIgnoreCase("localhost")) {
            computerName = java.net.InetAddress.getLocalHost().getCanonicalHostName();
        } 
    } catch (UnknownHostException e) {
        log.error("UnknownHostException detected in StartAction. ", e);
    }
    if(StringUtils.trim(computerName).length()>0) computerName = computerName.toUpperCase();
    System.out.println("computerName: " + computerName);
    return computerName;
}

but sometimes i am getting host name properly but some time not. I am getting Correct IP. What may be the reason for this? Why inetAddress.getHostName(); is failing to give host name some time? Your help is very appriciated.

+1  A: 

HttpServletRequest will return the IP address (either v4 or v6) of whoever is hitting your servlet. That address may or may not resolve to a valid hostname. InetAddress.getHostName() does a reverse DNS resolution of the IP address. It is not required that each IP address allocated maps back to a valid DNS entry. There are, in fact, a large percent of IP addresses in the world that will not resolve to a hostname.

You can see the same thing using the 'host' command on a linux box to look up the reverse DNS entry (if any) for a given IP address.

kkress
+1  A: 

The InetAddress.getHostName() function will return the host name if the InetAddress object was initialized with a host name. Otherwise, it'll do a reverse DNS lookup to get the host name.

To get this reverse DNS lookup to work, you'll need to make sure all of the clients on your intranet are configured with host names and that your DNS provider (e.g. your router) properly matches up the host names with its records. (Some routers can do this automatically.)

Tom
A: 

In order to get the hostname of windows machines, you will need to perform a reverse NetBIOS lookup on the IP address. Windows uses a system called WINS to provide hostnames to its computers. This system is based off NetBIOS.

If you don't want to try to find a specification of the protocol and implement it yourself, then you will want to execute the command nbtstat -A [ip address] if you are on Windows, or nmblookup -A [ip address] if you are on a Linux machine. If you are on a Linux machine, the Samba package will have to be installed as the nmblookup executable is installed on all Linux machines. You will then have to parse the output of that command to get the host name.

The alternative is, as stated before, try to find a specification of the protocol, and implement the part that you need to implement.

Thomas