package chat;

import java.io.*;


/**
 * Represents the client of a simple chat system, accepts user input
 * that is sent to the server to be broadcast to any other chat
 * clients currently running.
 */
public class Client extends util.net.Client
{
    /**
     * Create a client that connects to a server already running on
     * given hostName.
     */
    public Client (String hostName, int port)
    {
	super(hostName, port);
        addInputHandler(new util.net.PrintInputHandler());
    }

    /**
     * A generic read loop for client.
     */
    public void chatLoop ()
    {
	BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
	while (true)
	{
	    System.out.print(">> ");
	    System.out.flush();
	    try
	    {
		send(in.readLine());
	    }
	    catch (IOException e)
	    {}
	}
    }

    /**
     * Starts the client up.
     */
    public static void main (String[] args)
    {
        String hostname = "localhost";
        if (args.length > 0)
        {
            hostname = args[0];
        }
            
        new Client(hostname, Constants.PORT).chatLoop();
    }
}
