// Client.java
// Code by Jan Humble

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

class Client {

    public static void main (String [] argv) throws IOException {
	
	int portNr = Integer.parseInt(argv[0]);
	   
	System.out.println ("Connecting...");
	
	// Setup a socket to the host at this IP and Port
	Socket s = new Socket (InetAddress.getLocalHost (), portNr);
	
	PrintStream writer = new PrintStream (s.getOutputStream ());
	String request = "";
	
	// Read request from client
	request = Client.prompt();
	
 	// Interpret request
	while (!(request.equals("quit") || 
		 request.equals("exit"))) {
	    
	    if (request.equals("port"))
		{	
		    System.out.println ("Port Nr: " + s.getPort());
		}
	    else {
		// Wrap a PrintStream around the socket output stream and
		// send the request string. 
		
		writer.println (request);
	    }
	    request = Client.prompt();
	}	
	// Send disconnect request to server
	writer.println ("DISCONNECT");
    }
    
    
    public static String prompt() {
	
	
	byte [] byteInput = new byte[40];  // byte stream to read
	String input = "";
	System.out.print("Enter request > ");

	try {
	    System.in.read(byteInput);
	} catch (IOException ioe) {
	    System.out.print("No input read.");
	    
	}
	input = new String(byteInput);
	return input.trim();
    }
    
}

