Client computer name in java

17,509

Solution 1

   private String getHostName (InetAddress inaHost) throws UnknownHostException
    {
       try
       {
           Class clazz = Class.forName("java.net.InetAddress");
           Constructor[] constructors = clazz.getDeclaredConstructors();
           constructors[0].setAccessible(true);
           InetAddress ina = (InetAddress) constructors[0].newInstance();

           Field[] fields = ina.getClass().getDeclaredFields();
           for (Field field: fields)
           {
               if (field.getName().equals("nameService"))
               {
                   field.setAccessible(true);
                   Method[] methods = field.get(null).getClass().getDeclaredMethods();
                   for (Method method: methods)
                   {
                        if (method.getName().equals("getHostByAddr"))
                        {
                            method.setAccessible(true);
                            return (String) method.invoke(field.get (null), inaHost.getAddress());
                        }
                   }
               }
           }
       } catch (ClassNotFoundException cnfe) {
       } catch (IllegalAccessException iae) {
       } catch (InstantiationException ie) {
       } catch (InvocationTargetException ite) {
           throw (UnknownHostException) ite.getCause();
       }
       return null;
    }

above function returning host name correctly in Intranet. for local it will return localhost. To get the name for local host we use computerName = java.net.InetAddress.getLocalHost().getCanonicalHostName();

Solution 2

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

Solution 3

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.

Share:
17,509
vishnu
Author by

vishnu

Updated on August 02, 2022

Comments

  • vishnu
    vishnu almost 2 years

    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.

  • someone
    someone about 7 years
    how to initialize the InetAddress when invoking the method? null doesn't work