java类库详细讲解
源代码在线查看: gethostname.html
Getting the Hostname of an IP Address
(Java Developers Almanac Example)
BODY CODE {font-family: Courier, Monospace; font-size: 11pt} TABLE, BODY {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10pt} PRE {font-family: Courier, Monospace; font-size: 10pt} H3 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11pt} A.eglink {text-decoration: none} A:hover.eglink {text-decoration: underline} -->
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]
e145.
Getting the Hostname of an IP Address
This example attempts to retrieve the hostname for an IP address.
Note that getHostName() may not succeed, in which case it simply
returns the IP address.
try {
// Get hostname by textual representation of IP address
InetAddress addr = InetAddress.getByName("127.0.0.1");
// Get hostname by a byte array containing the IP address
byte[] ipAddr = new byte[]{127, 0, 0, 1};
addr = InetAddress.getByAddress(ipAddr);
// Get the host name
String hostname = addr.getHostName();
// Get canonical host name
String hostnameCanonical = addr.getCanonicalHostName();
} catch (UnknownHostException e) {
}
Related Examples
e144.
Getting the IP Address of a Hostname
e146.
Getting the IP Address and Hostname of the Local Machine
See also:
Datagram
Encodings
HTTP
Multicast
Sockets
URLs
© 2002 Addison-Wesley.