runnig

TestClient.java

May 7th, 2013
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.44 KB | None | 0 0
  1. /* compile: javac TestClient.java
  2.  * run: java TestClient host port
  3.  */
  4. import java.io.BufferedWriter;
  5. import java.io.IOException;
  6. import java.io.OutputStream;
  7. import java.io.OutputStreamWriter;
  8. import java.net.Socket;
  9.  
  10. public class TestClient {
  11.  
  12.     static final int MAX_PAYLOAD_SIZE = 128;
  13.     static final char PROTOCOL_VERSION = 0x7D;
  14.     static final int JAVA_CLIENT_ID = 0x01020304;
  15.  
  16.     static int pack(char buf[], char v, short t, long c)
  17.     {
  18.         buf[0] = v;
  19.         buf[1] = (char)((t >> 8) & 0xFF);
  20.         buf[2] = (char)((t >> 0) & 0xFF);
  21.  
  22.         buf[3] = (char)((c >> 24) & 0xFF);
  23.         buf[4] = (char)((c >> 16) & 0xFF);
  24.         buf[5] = (char)((c >> 8) & 0xFF);
  25.         buf[6] = (char)((c >> 0) & 0xFF);
  26.         return 7;
  27.     }
  28.  
  29.     static char random_char()
  30.     {
  31.         final int min_char = 1;
  32.         final int max_char = 255;
  33.  
  34.         return (char) (min_char + (Math.random() * (max_char - min_char) + min_char));
  35.     }
  36.     public static void main(String[] args) throws IOException {
  37.  
  38.         if( args.length < 2)
  39.         {
  40.             System.out.printf("Usage: java TestClient.class server port", args[0]);
  41.             return;
  42.         }
  43.         String host = args[0];
  44.         int port = Integer.parseInt(args[1]);
  45.         Socket s = new Socket(host, port);
  46.         BufferedWriter outp =
  47.             new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
  48.  
  49.  
  50.         char buf[] = new char[MAX_PAYLOAD_SIZE];
  51.  
  52.         for(short payload_size = 2; payload_size < MAX_PAYLOAD_SIZE; ++payload_size)
  53.         {
  54.             final short message_type = payload_size;
  55.             final int header_size = pack(buf, PROTOCOL_VERSION, message_type, JAVA_CLIENT_ID);
  56.             final int full_size = header_size + payload_size;
  57.  
  58.             outp.write(buf, 0, header_size);
  59.  
  60.  
  61.             System.out.printf("sending message of size %d\n", full_size);
  62.  
  63.             int bytes_sent = 0;
  64.  
  65.             while(bytes_sent < payload_size)
  66.             {
  67.                 int sending_size = Math.min(payload_size, buf.length);
  68.                 for(int pos = 0; pos < sending_size-1; ++pos)
  69.                 {
  70.                     buf[pos] = random_char();
  71.                 }
  72.                 buf[sending_size-1] = '\0';
  73.                 outp.write(buf, 0, sending_size);
  74.                 bytes_sent += sending_size;
  75.             }
  76.             outp.flush();
  77.  
  78.         }
  79.         s.close();
  80.         System.exit(0);
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment