KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test_util > SocksTest


1 package test_util;
2 import java.net.*;
3 import java.io.*;
4 import socks.*;
5
6 /** SOCKS aware echo client*/
7
8 public class SocksTest implements Runnable JavaDoc{
9   
10    private int port;
11    private InetAddress hostIP;
12
13    private Socket ss;
14    private InputStream in;
15    private OutputStream out;
16
17    private static final int BUF_SIZE = 1024;
18    static final int defaultProxyPort = 1080; //Default Port
19
static final String JavaDoc defaultProxyHost = "www-proxy"; //Default proxy
20

21    public SocksTest(String JavaDoc host,int port)
22       throws IOException,UnknownHostException,SocksException{
23       this.port = port;
24
25       ss = new SocksSocket(host, port);
26       out = ss.getOutputStream();
27       in = ss.getInputStream();
28       System.out.println("Connected...");
29       System.out.println("TO: "+host+":"+port);
30       System.out.println("ViaProxy: "+ss.getLocalAddress().getHostAddress()
31                                  +":"+ss.getLocalPort());
32
33    }
34
35    public void close()throws IOException{
36      ss.close();
37    }
38    public void send(String JavaDoc s) throws IOException{
39       out.write(s.getBytes());
40    }
41
42    public void run(){
43       byte[] buf = new byte[1024];
44       int bytes_read;
45       try{
46       while((bytes_read = in.read(buf)) > 0){
47          System.out.write(buf,0,bytes_read);
48       }
49       }catch(IOException io_ex){
50      io_ex.printStackTrace();
51       }
52    }
53
54    public static void usage(){
55       System.err.print(
56       "Usage: java SocksTest host port [socksHost socksPort]\n");
57    }
58
59
60    public static void main(String JavaDoc args[]){
61       int port;
62       String JavaDoc host;
63       int proxyPort;
64       String JavaDoc proxyHost;
65
66       if(args.length > 1 && args.length < 5){
67      try{
68
69          host = args[0];
70          port = Integer.parseInt(args[1]);
71
72          proxyPort =(args.length > 3)? Integer.parseInt(args[3])
73                                      : defaultProxyPort;
74
75          host = args[0];
76          proxyHost =(args.length > 2)? args[2]
77                                      : defaultProxyHost;
78
79          Proxy.setDefaultProxy(proxyHost,proxyPort,"KOUKY001");
80          //Proxy.setDefaultProxy(proxyHost,proxyPort);
81
Proxy.getDefaultProxy().setDirect(InetAddress.getByName("localhost"));
82
83
84          SocksTest st = new SocksTest(host,port);
85          Thread JavaDoc thread = new Thread JavaDoc(st);
86          thread.start();
87
88          BufferedReader in = new BufferedReader(
89                  new InputStreamReader(System.in));
90              String JavaDoc s;
91
92              s = in.readLine();
93          while(s != null){
94                 st.send(s+"\r\n");
95         //try{
96
//Thread.currentThread().sleep(10);
97
//}catch(InterruptedException i_ex){
98
//}
99
s = in.readLine();
100          }
101          st.close();
102          System.exit(1);
103
104      }catch(SocksException s_ex){
105        System.err.println("SocksException:"+s_ex);
106        s_ex.printStackTrace();
107        System.exit(1);
108      }catch(IOException io_ex){
109        io_ex.printStackTrace();
110        System.exit(1);
111      }catch(NumberFormatException JavaDoc num_ex){
112        usage();
113        num_ex.printStackTrace();
114        System.exit(1);
115      }
116
117       }else{
118     usage();
119       }
120    }
121
122 }//End of class
123
Popular Tags