KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > application > utilities > command > AceCommandClient


1 /*
2  * AceCommandClient.java
3  *
4  * Created on August 24, 2002, 6:50 AM
5  */

6
7 package com.quikj.application.utilities.command;
8
9 import java.net.*;
10 import java.io.*;
11
12 /**
13  *
14  * @author amit
15  */

16 public class AceCommandClient extends Thread JavaDoc
17 {
18     private String JavaDoc command = null;
19     private Socket socket = null;
20     private InputStream is;
21     private DataOutputStream os;
22     
23     /** Creates a new instance of AceCommandClient */
24     public AceCommandClient(String JavaDoc host, int port, String JavaDoc command)
25     throws UnknownHostException, IOException
26     {
27         super();
28         this.command = command;
29         System.out.println("Connecting to host: " + host + ":" + port + "....");
30         socket = new Socket(host, port);
31         socket.setSoTimeout(5000);
32         
33         is = socket.getInputStream();
34         os = new DataOutputStream(socket.getOutputStream());
35         
36         System.out.println("Connected to host: " + host + ":" + port);
37     }
38     
39     public void dispose()
40     {
41         try
42         {
43             socket.close();
44         }
45         catch (IOException ex)
46         {
47             ;
48         }
49     }
50     
51     public void run()
52     {
53         try
54         {
55             sleep (1000);
56             // send the command to the other side
57
os.writeBytes(command + '\n');
58             
59             try
60             {
61                 // wait for a response
62
int c = -1;
63                 do
64                 {
65                     c = is.read();
66                     if (c > 0)
67                     {
68                         System.out.print((char)c);
69                     }
70                 }
71                 while (c > 0);
72                 System.out.println("Connection closed by the peer");
73             }
74             catch (InterruptedIOException ex)
75             {
76                 ;
77             }
78             
79             dispose();
80             return;
81             
82         }
83         catch (Exception JavaDoc ex)
84         {
85             System.err.println(ex.getClass().getName() + ": " + ex.getMessage());
86             dispose();
87         }
88     }
89     
90     public static void main(String JavaDoc[] args)
91     {
92         if (args.length < 3)
93         {
94             System.err.println ("Usage: AceCommandClient <host> <port> <command>");
95             System.exit(1);
96         }
97         
98         try
99         {
100             AceCommandClient client = new AceCommandClient(args[0],
101             Integer.parseInt(args[1]), args[2]);
102             client.start();
103
104             client.join();
105             System.exit(0);
106         }
107         catch (Exception JavaDoc ex)
108         {
109             System.err.println(ex.getClass().getName() + ": " + ex.getMessage());
110             System.exit(1);
111         }
112     }
113 }
114
Popular Tags