/Users/lyon/j4p/src/net/dns/Dns.java
|
1 package net.dns;
2
3 import java.net.InetAddress;
4 import java.net.UnknownHostException;
5
6 public class Dns {
7 public static void main(String args[]) {
8 print(getNumericAddress("www.fairfield.edu"));
9 System.out.println(getHostName("12.23.55.212"));
10 //byte b = (byte)0xff;
11 //System.out.println(b);
12 //System.out.println((int)(0xff&b));
13 //System.out.println("twos comp of -1="+((~-1)+1));
14
15 // The above outputs:
16 // 12.23.55.212
17 // www.fairfield.edu
18 }
19
20
21 /**
22 * get the host name for this IP addBk.address.
23 *
24 */
25 public static String getHostName(String s) {
26 try {
27 InetAddress ia = InetAddress.getByName(s);
28 return ia.getHostName();
29 } catch (UnknownHostException e) {
30 }
31 return null;
32 }
33
34 /**
35 * map the host name to an IP addBk.address.
36 * return the address of the fully qualified
37 * domain name. For example:
38 * 192.68.1.90 = show.docjava.com
39 * Failure occurs if the DNS is down!
40 */
41 public static byte[] getNumericAddress(String name) {
42 InetAddress ia =
43 null;
44 try {
45 ia = InetAddress.getByName(name);
46 } catch (UnknownHostException e) {
47 }
48 return ia.getAddress();
49 }
50
51 /**
52 * print out a well formatted dot notation for an array
53 * of byte. For example: 192.168.1.1
54 */
55 public static void print(byte IP[]) {
56 for (int index = 0; index < IP.length; index++) {
57 if (index > 0) System.out.print(".");
58 System.out.print(((int) IP[index]) & 0xff);
59 }
60 System.out.println();
61 }
62 }