import java.io.*;
import java.net.*;

class AddClient {
    public static void main( String[] args ) {
        if ( args.length != 4 ) {
	    System.out.println("usage: java AddClient host port range bufsize");
	    System.exit(0);
        }
        int range = 0;
	int port = 0;
	String host = null;
        int bufSize = 0;
	try {
	    host = args[0];
	    port = Integer.parseInt( args[1] );
            range = Integer.parseInt( args[2] );
            bufSize = Integer.parseInt( args[3] );
	}
	catch( NumberFormatException e ) {
	    System.out.println("bad port number or range");
	    System.exit(0);
	}

        try {
	    /* determine the address of the server and connect to it */
	    InetAddress server = InetAddress.getByName( host );
	    Socket sock = new Socket( server, port );
            OutputStream out = sock.getOutputStream();

            if ( bufSize != 0 ) {
                out = new BufferedOutputStream( out, bufSize );
            }

            DataOutputStream dout = new DataOutputStream( out );
            DataInputStream din =
                new DataInputStream( sock.getInputStream() );

            long t = System.currentTimeMillis();
            /* tx size */
            dout.writeInt( range );
            /* tx ints to add */
            for( int i = 0 ; i < range; i++ ) {
                dout.writeInt( i );
            }
            dout.flush();
            /* retrieve result */
            int result = din.readInt();
            long dt = System.currentTimeMillis() - t;
            System.out.println("result is " + result );
            System.out.println("time is " + dt );
            /* tell the server that we are done */
            dout.writeInt( -1 );
            dout.flush();
	    sock.close();
            dout.close();
            din.close();
	}
	catch( UnknownHostException e ) {
	    System.out.println("bad host name");
	    System.exit(0);
	}
	catch( IOException e ) {
	    System.out.println("io error:" + e);
	    System.exit(0);
	}
    }
}
