1 import java.io.*;
 2 import java.net.*;
 3 
 4 /*
 5 Request:
 6  GET http://192.168.1.2:8888/index.html?crap=1014212221 HTTP/1.1
 7 
 8 Response:
 9  Cache-Control: no-cache, no-store, must-revalidate
10  Expires: 0
11  Proxy-Connection: close
12 */
13 public class EchoClient extends HttpReader {
14 
15    public EchoClient(String proxyHost, int port, String destinationUrl) throws IOException {
16       super("EchoClient");
17 
18       Socket echoSocket = null;
19       PrintWriter out = null;
20       BufferedInputStream in = null;
21 
22       try {
23          echoSocket = new Socket(proxyHost, port);
24          echoSocket.setTcpNoDelay(true);
25          echoSocket.setSoLinger(true, 2000);
26          //echoSocket.setSoTimeout(1000*60*);
27          echoSocket.setKeepAlive(true); // JDK 1.3
28          System.out.println("\n*** Connected to proxy=" + proxyHost + " on proxyPort=" + port + " accessing " + destinationUrl);
29          out = new PrintWriter(echoSocket.getOutputStream(), true);
30          in = new BufferedInputStream(echoSocket.getInputStream());
31          int count = 10;
32          for (int ii=0; ii<count; ii++) {
33             if ((count % 10) == 9) {
34                try { Thread.currentThread().sleep(20); } catch(Exception e) { }
35             }
36             System.out.println("\nSending POST #" + ii);
37             String resp = "        16UAL #" + ii%10;
38             String header = getPostHeader(destinationUrl, resp.length());
39             out.print(header);
40             out.print(resp);
41             out.flush();
42 
43             // +1 for the response
44             for (int jj=0; jj<COUNT_CB+1; jj++) {
45                System.out.println("\n*** Waiting for data " + (ii+1)*jj + " [" + new java.util.Date().toString() + "]");
46                byte[] data = read(in);
47                System.out.println("*** Received: '" + new String(data) + "'");
48             }
49             try { Thread.currentThread().sleep(500); } catch(Exception e) { }
50          }
51          /*
52          while (true) {
53             System.out.println("\n*** Waiting for callback data");
54             byte[] data = read(in);
55             System.out.println("*** Received Callback: '" + new String(data) + "'");
56          }
57          */
58       } catch (NumberFormatException e) {
59          System.err.println("The message is corrupted, can't parse content length: " + e.toString());
60       } catch (UnknownHostException e) {
61          System.err.println("Don't know about proxyHost: " + proxyHost);
62       } catch (IOException e) {
63          System.err.println("Couldn't get I/O for the connection to: " + proxyHost);
64       }
65       finally {
66          if (out != null) out.close();
67          if (in != null) in.close();
68          if (echoSocket != null) echoSocket.close();
69          System.out.println("Connection removed!\n");
70       }
71    }
72 
73 
74    public static void main(String[] args) throws IOException {
75       System.out.println("Usage: java EchoClient <proxyHost> <port> <destinationUrl>\nExample:   java EchoClient 192.168.1.2 3128 http://194.121.221.46:8080\n");
76       
77       String destinationUrl = "http://194.121.221.46:8080";   //"http://192.168.1.2:8080";
78       String proxyHost = "192.168.1.2";
79       int port = 3128;
80       try {
81          if (args.length > 0)
82             proxyHost = args[0];
83          if (args.length > 1)
84             port = Integer.parseInt(args[1]);
85          if (args.length > 2)
86             destinationUrl = args[2];
87 
88          new EchoClient(proxyHost, port, destinationUrl);
89       }
90       catch (Throwable e) {
91          System.out.println("STARTUP ERROR: " + e.toString());
92          e.printStackTrace();
93       }
94    }
95 }


syntax highlighted by Code2HTML, v. 0.9.1