forked from CodeYourFuture/workshop-code
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDnsResolver.java
More file actions
31 lines (28 loc) · 1.2 KB
/
Copy pathDnsResolver.java
File metadata and controls
31 lines (28 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import java.net.InetAddress;
import java.net.UnknownHostException;
class DnsResolver {
public static void main(String[] args) {
if (args.length != 1) {
System.err.printf("Expected exactly one argument, but got %d%n", args.length);
System.exit(1);
}
String host = args[0];
String[] parts = host.split("\\.");
System.out.printf("The top-level domain of the host %s is %s%n", host, parts[parts.length - 1]);
try {
InetAddress[] ipAddresses = InetAddress.getAllByName(host);
if (ipAddresses.length == 0) {
System.out.printf("The host %s did not resolve to any IP addresses%n", host);
} else {
String pluralSuffix = ipAddresses.length == 1 ? "" : "es";
System.out.printf("The host %s resolved to the following IP address%s:%n", host, pluralSuffix);
}
for (InetAddress ipAddress : ipAddresses) {
System.out.println(ipAddress.getHostAddress());
}
} catch (UnknownHostException e) {
System.err.printf("Failed to resolve %s: %s%n", host, e.getMessage());
System.exit(1);
}
}
}