/*This code written by Nishant Agarwal
  Jul 12th, 2004
  nishant@purecode.us
*/

package client; 

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

public class MyClient {
    public static void main(String[] argv) throws IOException {
       
        //Resources we need
        Socket sock       = null;
        BufferedReader in = null;
        PrintWriter out   = null; 

        //Now initialize the resources
        try {
            sock = new Socket("nunki.usc.edu", 51211);
            in   = new BufferedReader(new InputStreamReader(sock.getInputStream()));
            out  = new PrintWriter(sock.getOutputStream(), true);
	} catch (UnknownHostException e) {
            System.err.println("Could not connect in Client: " +
                               e.getMessage());
            e.printStackTrace();
            System.exit(-1);
	} catch (IOException e) {
            System.err.println("Could not open streams in Client: " +
                               e.getMessage());
            e.printStackTrace();
            System.exit(-1);
	}
   
        //talk to server according to protocol
        Protocol protocol = new MyProtocol(in, out);
        try {
            protocol.clientAddRequest(argv[0], argv[1]);
        } catch (IOException e) {
            System.err.println("Client stream error: " +
                               e.getMessage());
            e.printStackTrace();
	} catch (ArrayIndexOutOfBoundsException e) {
            System.err.println("Please give two integers as arguments: " +
                               e.getMessage());         
	}

        //Close the resources
        out.close();
        in.close();
        sock.close();
    }
}
