Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* compile: javac TestClient.java
- * run: java TestClient host port
- */
- import java.io.BufferedWriter;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.io.OutputStreamWriter;
- import java.net.Socket;
- public class TestClient {
- static final int MAX_PAYLOAD_SIZE = 128;
- static final char PROTOCOL_VERSION = 0x7D;
- static final int JAVA_CLIENT_ID = 0x01020304;
- static int pack(char buf[], char v, short t, long c)
- {
- buf[0] = v;
- buf[1] = (char)((t >> 8) & 0xFF);
- buf[2] = (char)((t >> 0) & 0xFF);
- buf[3] = (char)((c >> 24) & 0xFF);
- buf[4] = (char)((c >> 16) & 0xFF);
- buf[5] = (char)((c >> 8) & 0xFF);
- buf[6] = (char)((c >> 0) & 0xFF);
- return 7;
- }
- static char random_char()
- {
- final int min_char = 1;
- final int max_char = 255;
- return (char) (min_char + (Math.random() * (max_char - min_char) + min_char));
- }
- public static void main(String[] args) throws IOException {
- if( args.length < 2)
- {
- System.out.printf("Usage: java TestClient.class server port", args[0]);
- return;
- }
- String host = args[0];
- int port = Integer.parseInt(args[1]);
- Socket s = new Socket(host, port);
- BufferedWriter outp =
- new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
- char buf[] = new char[MAX_PAYLOAD_SIZE];
- for(short payload_size = 2; payload_size < MAX_PAYLOAD_SIZE; ++payload_size)
- {
- final short message_type = payload_size;
- final int header_size = pack(buf, PROTOCOL_VERSION, message_type, JAVA_CLIENT_ID);
- final int full_size = header_size + payload_size;
- outp.write(buf, 0, header_size);
- System.out.printf("sending message of size %d\n", full_size);
- int bytes_sent = 0;
- while(bytes_sent < payload_size)
- {
- int sending_size = Math.min(payload_size, buf.length);
- for(int pos = 0; pos < sending_size-1; ++pos)
- {
- buf[pos] = random_char();
- }
- buf[sending_size-1] = '\0';
- outp.write(buf, 0, sending_size);
- bytes_sent += sending_size;
- }
- outp.flush();
- }
- s.close();
- System.exit(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment