import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;

public class Exec12_2Client {
  public static void main(String[] args) {
    if (args.length!=1) {
      System.out.println("Usage: java Exec12_2Client serverIP");
      System.exit(-1);
    }

    theFrame=new ClientFrame(args[0]);
    theFrame.setTitle("Client");
    theFrame.setSize(400, 300);
    theFrame.show();
  }

  protected static Frame theFrame;
}

class ClientFrame extends Frame implements ActionListener, Runnable {
  public ClientFrame(String serverIP) {
    setLayout(new BorderLayout());

    northPanel.setLayout(new FlowLayout());
    northPanel.add(inputArea);
    northPanel.add(sendButton);
    sendButton.addActionListener(this);
    add("North", northPanel);

    centerPanel.setLayout(new FlowLayout());
    centerPanel.add(displayArea);
    displayArea.setEditable(false);
    add("Center", centerPanel);

    southPanel.setLayout(new FlowLayout());
    southPanel.add(clearInputButton);
    southPanel.add(clearDisplayButton);
    southPanel.add(quitButton);
    clearInputButton.addActionListener(this);
    clearDisplayButton.addActionListener(this);
    quitButton.addActionListener(this);
    add("South", southPanel);

    try {
      socket=new Socket(serverIP, 8008);
      in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
      out=new PrintWriter(new PrintWriter(new OutputStreamWriter(socket.getOutputStream())));

    } catch (IOException e) {
      e.printStackTrace();
      System.exit(-1);
    }

    // Using a seperate thread to receive and show the message sent from server
    readThread=new Thread(this);
    readThread.start();
  }

  public void actionPerformed(ActionEvent e) {
    if (e.getSource()==sendButton) {
      // When send button is clicked, send the message to server
      out.println(inputArea.getText());
      out.flush();
    } else if (e.getSource()==clearInputButton) {
      inputArea.setText("");
    } else if (e.getSource()==clearDisplayButton) {
      displayArea.setText("");
    } else if (e.getSource()==quitButton) {
      hide();
      try {
        socket.close();
      } catch (IOException e1) {
        e1.printStackTrace();
      }

      System.exit(0);
    }
  }

  public void run() {
    // Show the message sent from the server
    try {
      while (true) {
        String line=in.readLine();

        if (line==null) {
          break;
        }

        displayArea.append(line+"\r\n");
      }
    } catch (SocketException e) {
    } catch (IOException e) {
      e.printStackTrace();
      System.exit(-1);
    }
  }

  protected Socket socket;
  protected BufferedReader in;
  protected PrintWriter out;

  protected Thread readThread;

  protected Panel northPanel=new Panel();
  protected TextArea inputArea=new TextArea("", 3, 40);
  protected Button sendButton=new Button("Send");

  protected Panel centerPanel=new Panel();
  protected TextArea displayArea=new TextArea("", 8, 40);

  protected Panel southPanel=new Panel();
  protected Button clearInputButton=new Button("Clear Input");
  protected Button clearDisplayButton=new Button("Clear Display");
  protected Button quitButton=new Button("Quit");
}

