KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > fwhois


1 /*
2  * Copyright 2001-2005 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package examples;
17
18 import java.io.IOException JavaDoc;
19 import java.net.InetAddress JavaDoc;
20 import java.net.UnknownHostException JavaDoc;
21 import org.apache.commons.net.WhoisClient;
22
23 /***
24  * This is an example of how you would implement the Linux fwhois command
25  * in Java using NetComponents. The Java version is much shorter.
26  * <p>
27  ***/

28 public final class fwhois
29 {
30
31     public static final void main(String JavaDoc[] args)
32     {
33         int index;
34         String JavaDoc handle, host;
35         InetAddress JavaDoc address = null;
36         WhoisClient whois;
37
38         if (args.length != 1)
39         {
40             System.err.println("usage: fwhois handle[@<server>]");
41             System.exit(1);
42         }
43
44         index = args[0].lastIndexOf("@");
45
46         whois = new WhoisClient();
47         // We want to timeout if a response takes longer than 60 seconds
48
whois.setDefaultTimeout(60000);
49
50         if (index == -1)
51         {
52             handle = args[0];
53             host = WhoisClient.DEFAULT_HOST;
54         }
55         else
56         {
57             handle = args[0].substring(0, index);
58             host = args[0].substring(index + 1);
59         }
60
61         try
62         {
63             address = InetAddress.getByName(host);
64         }
65         catch (UnknownHostException JavaDoc e)
66         {
67             System.err.println("Error unknown host: " + e.getMessage());
68             System.exit(1);
69         }
70
71         System.out.println("[" + address.getHostName() + "]");
72
73         try
74         {
75             whois.connect(address);
76             System.out.print(whois.query(handle));
77             whois.disconnect();
78         }
79         catch (IOException JavaDoc e)
80         {
81             System.err.println("Error I/O exception: " + e.getMessage());
82             System.exit(1);
83         }
84     }
85
86 }
87
Popular Tags