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


public class Client {

    OutputStream out      = null;
    InputStream  in       = null;


    public static void main( String[] args ) throws IOException {
	
	Client client = new Client();
	String ibuf, obuf;
	boolean more;
	int i=-1;
	String host = args[0];
	int port = ( Integer.valueOf( args[1] )).intValue();
	Socket mySocket = null;
	
	try {
	    // make connection to server socket
	    mySocket = new Socket( host,port );
	    client.out = mySocket.getOutputStream();
	    client.in  = mySocket.getInputStream();
	}
	catch ( UnknownHostException uhx ) {
	    System.err.println( uhx );
	    System.exit( 1 );
	} 
	catch ( IOException iox ) {
	    System.err.println( iox );
	    System.exit( 1 );
	}
	
	// open stream to read user's input
	DataInputStream stdin = new DataInputStream( System.in );
	
	// loop while reading/writing on the socket
	more = true;
	while ( more ) {
	    System.out.print( "enter message to send (q to quit): " );
	    obuf = stdin.readLine();
	    if ( obuf.charAt( 0 ) == 'q' ) {
		more = false;
	    }
	    else {
		client.sendMsg( obuf );
		System.out.println( "client: waiting for msg from server" );
		if (( ibuf = client.readMsg()) == null ) {
		    more = false;
		}
	    }
	}
	
	// close everything
	client.out.close();
	client.in.close();
	stdin.close();
	mySocket.close();
	
  } /* end of main() */



    void sendMsg( String msg ) throws IOException {
	byte i = (byte)msg.length();
	System.out.println( "client: sending message [" + msg + "] " + i );
	out.write( i );
	for ( int j=0; j<i; j++ ) {
	    out.write( (int)msg.charAt( j ));
	}
    } /* end of sendMsg() */
    

    String readMsg() throws IOException {
	String msg = null;
	int i;
	if (( i = in.read()) > 0 ) {
	    msg = new String();
	    int k;
	    for ( int j=0; j<i; j++ ) {
		if (( k = in.read()) == -1 ) {
		    msg = null;
		    break;
		}
		msg += (char)k;
	    }
	}
	if ( msg != null ) {
	    System.out.println( "client: read message [" + msg + "]" );
	}
	return( msg );
    } /* end of readMsg() */


} /* end of Client class */
