KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > TestClient


1 package test;
2 import java.io.*;
3 import java.net.*;
4 import socks.*;
5
6 public class TestClient extends TestService{
7    /** Proxy which should be used*/
8    Proxy proxy;
9    /** Host on which TestServer is running*/
10    String JavaDoc testHost;
11
12    int timeout = 15000;
13    int acceptTimeout = 0;
14
15    BufferedReader in;
16    Writer out;
17
18    public TestClient(Proxy p,String JavaDoc testHost){
19       this.proxy = p;
20       this.testHost = testHost;
21       if(log == null) log = System.out;
22    }
23
24    public void start(){
25       connectTests(true);
26       acceptTests(true);
27       udpTests(true);
28
29       connectTests(false);
30       acceptTests(false);
31       udpTests(false);
32    }
33
34    void connectTests(boolean useString){
35       try{
36          open(ECHO, useString);
37          testEcho();
38          s.close();
39
40          open(DISCARD, useString);
41          testDiscard();
42          s.close();
43
44          open(CHARGEN, useString);
45
46          for(int i = 0; i< 3;){
47             try{
48                testChargen();
49                break;
50             }catch(InterruptedIOException ioe){
51                log("IO interrupted:"+i);
52                i++;
53             }
54          }
55
56          s.close();
57
58       }catch(IOException ioe){
59          ioe.printStackTrace();
60       }
61
62    }
63
64    void acceptTests(boolean useString){
65       try{
66          testAccept(ECHO, useString);
67          testEcho();
68          s.close();
69
70          testAccept(DISCARD, useString);
71          testDiscard();
72          s.close();
73
74          testAccept(CHARGEN, useString);
75
76          for(int i = 0; i< 3;){
77             try{
78                testChargen();
79                break;
80             }catch(InterruptedIOException ioe){
81                log("IO interrupted:"+i);
82                i++;
83             }
84          }
85          s.close();
86
87       }catch(IOException ioe){
88          ioe.printStackTrace();
89       }
90    }
91
92    void udpTests(boolean useString){
93       log("Udp tests are not yet implemented");
94    }
95
96    void testEcho() throws IOException{
97       log("Testing echo.");
98       for(int i=0;i<5;++i){
99          out.write("String number "+i+"\r\n");
100          out.flush();
101          log("Echo:"+in.readLine());;
102       }
103       log("Echo finished");
104    }
105
106    void testDiscard() throws IOException{
107       log("Testing discard");
108       for(int i =0; i < 5;++i){
109          log("Sending discard message:"+i);
110          out.write("Discard message:"+i+"\r\n");
111          out.flush();
112       }
113       log("Discard finished");
114    }
115
116    void testChargen() throws IOException{
117       log("Testing chargen");
118       String JavaDoc s;
119       s = in.readLine();
120       while(s!=null){
121          log("ChGen:"+s);
122          s = in.readLine();
123       }
124       log("Chargen finished.");
125    }
126
127    void testAccept(int service,boolean useString)throws IOException{
128       open(CONNECT,useString);
129
130       log("Testing accept");
131       ServerSocket ss;
132
133       if(useString)
134         ss = new SocksServerSocket(proxy,testHost,servicePorts[service]);
135       else
136         ss = new SocksServerSocket(proxy,InetAddress.getByName(testHost),
137                                    servicePorts[service]);
138       log("Listenning on "+ss.getInetAddress()+":"+ss.getLocalPort());
139       ss.setSoTimeout(acceptTimeout);
140  
141       out.write(""+ss.getLocalPort()+" "+service+"\r\n");
142       out.flush();
143
144       String JavaDoc line = in.readLine();
145       if(line != null){
146         log("Accept failed:"+line);
147       }
148
149       s.close();
150
151       s = ss.accept();
152       log("Accepted:"+s);
153
154       s.setSoTimeout(timeout);
155
156       out = new OutputStreamWriter(s.getOutputStream());
157       in = new BufferedReader(new InputStreamReader(s.getInputStream()));
158
159       ss.close();
160    }
161
162    void open(int service,boolean useString) throws IOException{
163
164       if(!useString){
165          s = new SocksSocket(proxy,InetAddress.getByName(testHost),
166                                    servicePorts[service]);
167       }else{
168          s = new SocksSocket(proxy,testHost,servicePorts[service]);
169       }
170
171       s.setSoTimeout(timeout);
172
173       out = new OutputStreamWriter(s.getOutputStream());
174       in = new BufferedReader(new InputStreamReader(s.getInputStream()));
175    }
176
177    //Main function
178
///////////////
179

180    static void usage(){
181       System.err.println(
182       "Usage: java Testclient testhost proxy [directhosts]");
183    }
184
185    static Proxy initProxy(String JavaDoc ps){
186       java.util.StringTokenizer JavaDoc st = new java.util.StringTokenizer JavaDoc(ps,",;");
187       Proxy proxy = null;
188       while(st.hasMoreElements()){
189          String JavaDoc entry = st.nextToken();
190          Proxy p = Proxy.parseProxy(entry);
191          if( p == null){
192            log("Proxy "+entry+" invalid.");
193            return null;
194          }
195          p.setChainProxy(proxy);
196          proxy = p;
197       }
198       return proxy;
199    }
200    static void addDirectHosts(Proxy p, String JavaDoc directHosts){
201       java.util.StringTokenizer JavaDoc st = new java.util.StringTokenizer JavaDoc(
202                                          directHosts,",;");
203
204       while(st.hasMoreElements()){
205          String JavaDoc entry = st.nextToken();
206          log("Adding direct host:"+entry);
207          p.addDirect(entry);
208       }
209    }
210
211    public static void main(String JavaDoc[] argv){
212       if(argv.length < 2){
213         usage();
214         return;
215       }
216
217       log = System.out;
218
219       String JavaDoc testHost = argv[0];
220       String JavaDoc proxyHost = argv[1];
221       String JavaDoc directHosts = argv.length >2 ? argv[2] : null;
222
223       Proxy p = initProxy(proxyHost);
224       if(p == null){
225          log("Can't init proxy.");
226          return;
227       }
228       if(directHosts!=null) addDirectHosts(p,directHosts);
229
230       if(p instanceof Socks5Proxy)
231          ((Socks5Proxy) p).resolveAddrLocally(false);
232
233       TestClient tc = new TestClient(p,testHost);
234       tc.start();
235       
236    }
237 }
238
Popular Tags