/* This is the client program. It creates a Socket object to establish 
the connection with the server program. It then creates its input and
output streams using the Socket. It reads lines from standard input,
writes them to the server program, reads the reply from the server,
and writes that to standard output.  The client exits when it detects
end of file on either the Socket input stream or on the standard
input stream.
Host and port number are passed as command line arguments.
From Java in a Nutshell, 1st Ed., O'Reilly
*/
import java.io.*;
import java.net.*;
public class Client
{
  public static final int DEFAULT_PORT = 6789;
  
  // usage------------------------------------------------------
  // specifies correct usage and exits if host name missing
  
  protected static void usage()
  {
    System.out.println("Usage: java Client <hostname> [<port>]");
    System.exit(1);
  }
    
  // main ------------------------------------------------------
  // All the work is done here
  
  public static void main(String args[])
  {
    int port = DEFAULT_PORT;
    Socket s = null;
    if (args.length == 0)
      usage();
      
    if (args.length == 1)   // passed server only
      port = DEFAULT_PORT;
    else
    {
      try
      {
        port = Integer.parseInt(args[1]);
      }
      catch (NumberFormatException e)
      {
      	usage();
      }
     }
     
     try
     {
     	// create a socket to communicate with specified host and port
     	s = new Socket(args[0], port);
     	
     	// create streams for reading and writing lines of text to this socket
        PrintWriter out = new PrintWriter(s.getOutputStream());     	
     	InputStreamReader in = new InputStreamReader(s.getInputStream());
     	BufferedReader bufReader = new BufferedReader(in);
     	
     	
       	// create a reader to read lines of text from keyboard
     	InputStreamReader kin = new InputStreamReader(System.in);
     	BufferedReader kbuf = new BufferedReader(kin);
     	 
     	// tell user we are connected
     	System.out.println("Connected to " + s.getInetAddress() + " on port " + s.getPort());
     	
     	String line = null;
     	
     	while (true)
     	{
     	  // prompt user
     	  System.out.println("Enter a line: ");
     	  line = kbuf.readLine();
     	  if (line == null)
     	    break;
     	  
     	  //send it to the server
     	  out.println(line);
     	  out.flush();
     	  
     	  //read server response
     	  line = bufReader.readLine();
     	  
     	  //check if connection is closed (i.e., EOF)
     	  if (line == null)
     	  {
     	    System.out.println("Connection closed by server");
     	    break;
     	  }
     	  
     	  //write response to console
     	  System.out.println(line);
     	} // end while
     } // end try
     catch (IOException e) {System.err.println(e);}
     
     //always be sure to close the socket
     finally
     {
       try
       {
       	 if (s != null)
       	   s.close();
       }
       catch (IOException e2) {}
     }
  } // end main
} // end client