Java Reference
In-Depth Information
4
public static void main(String[] args) {
5
6
// Get name and IP address of the local host
7
try {
8
InetAddress address = InetAddress.getLocalHost();
9
System.out.println("Local Host:");
10
System.out.println("\t" + address.getHostName());
11
System.out.println("\t" + address.getHostAddress());
12
} catch (UnknownHostException e) {
13
System.out.println("Unable to determine this host's address");
14
}
15
16
for(inti=0;i<args.length; i++) {
17
// Get name(s)/address(es) of hosts given on command line
18
try {
19
InetAddress[] addressList = InetAddress.getAllByName(args[i]);
20
System.out.println(args[i] + ":");
21
// Print the first name. Assume array contains at least one entry.
22
System.out.println("\t" + addressList[0].getHostName());
23
for(intj=0;j<addressList.length; j++)
24
System.out.println("\t" + addressList[j].getHostAddress());
25
} catch (UnknownHostException e) {
26
System.out.println("Unable to find address for " + args[i]);
27
}
28
}
29
}
30 }
InetAddressExample.java
1. Print information about the local host: lines 6-14
Create an InetAddress instance for the local host: line 8
Print the local host information: lines 9-11
getHostName() and getHostAddress() return a string for the host name and IP address,
respectively.
2. Request information for each host specified on the command line: lines 16-28
Create an array of InetAddress instances for the specified host: line 19
InetAddress.getAllByName() returns an array of InetAddress instances, one for each
of the specified host's addresses.
Print the host information: lines 22-24
To use this application to find information about the local host and the publisher's Web server
( www.mkp.com ), do the following:
Search WWH ::




Custom Search