Wednesday, July 15, 2009

Getting the IP Address of a Hostname

Getting the IP Address of a Hostname

        try {
                InetAddress addr = InetAddress.getByName("google.com");
                byte[] ipAddr = addr.getAddress();

                // Convert to dot representation
                String ipAddrStr = "";
                for (int i=0; i<ipAddr.length; i++) {
                    if (i > 0) {
                        ipAddrStr += ".";
                    }
                    ipAddrStr += ipAddr[i]&0xFF;
                }
            System.out.println("ipAddrStr = " + ipAddrStr);
            } catch (UnknownHostException e) {
            }

No comments: