The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.net  [27 examples] > Hostnames and IP Addresses  [3 examples]

e144. Getting the IP Address of a Hostname

    try {
        InetAddress addr = InetAddress.getByName("javaalmanac.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;
        }
    } catch (UnknownHostException e) {
    }

 Related Examples
e145. Getting the Hostname of an IP Address
e146. Getting the IP Address and Hostname of the Local Machine

See also: Datagram    Encodings    HTTP    Multicast    Sockets    URLs   


© 2002 Addison-Wesley.