/*
    This class is a basic extension of the Applet class.  It would generally
    be used as the main class with a Java browser or the AppletViewer.  But
    an instance can be added to a subclass of Container.  To use this applet
    with a browser or the AppletViewer, create an html file with the
    following code:

    <HTML>
    <HEAD>
    <TITLE> CSocketClient </TITLE>
    </HEAD>
    <BODY>

    <APPLET CODE="CSocketClient.class" WIDTH=283 HEIGHT=190></APPLET>

    </BODY>

    </HTML>

    You can add controls to CSocketClient with Cafe Studio.
    (Menus can be added only to subclasses of Frame.)
 */

import java.awt.*;
import java.applet.*;
import java.io.*;
import java.net.*;

public class CSocketClient extends Applet {

    public void init() {

        super.init();

        //{{INIT_CONTROLS
        setLayout(null);
        resize(281,189);
        SendButton=new Button("Send");
        SendButton.setFont(new Font("Dialog",Font.PLAIN,10));
        add(SendButton);
        SendButton.reshape(182,8,91,30);
        ChatArea=new TextArea(8,32);
        ChatArea.setFont(new Font("Dialog",Font.PLAIN,10));
        add(ChatArea);
        ChatArea.reshape(7,45,266,135);
        ChatField=new TextField(19);
        add(ChatField);
        ChatField.reshape(7,8,161,30);
        //}}
        ChatArea.setEditable(false);
    }

    public boolean handleEvent(Event event) {
        if (event.id == Event.ACTION_EVENT && (event.target == SendButton || event.target == ChatField)) {
                clickedSendButton();
                return true;
        }

        return super.handleEvent(event);
    }

    //{{DECLARE_CONTROLS
    Button SendButton;
    TextArea ChatArea;
    TextField ChatField;
    //}}
    public void clickedSendButton() {
        // to do: put event handler code here.
		SymCafeSpawnClient client=new SymCafeSpawnClient(ChatArea,ChatField);
		client.start();
    }
}

class SymCafeSpawnClient extends Thread
{
 	java.awt.TextArea textArea1;
	java.awt.TextField textField1;

    SymCafeSpawnClient(TextArea myTextArea,
        TextField myTextField) {
        textArea1=myTextArea;
        textField1=myTextField;
    }

  public void run() {

    try {
		// Open a new socket connection
		Socket ClientSocket=new Socket("localhost",7523);

		// Start a new thread
	    SymCafeClientHandleConnection thread=
		  new SymCafeClientHandleConnection(ClientSocket,
			textArea1, textField1);
		thread.start();

      } catch (IOException e) {
		System.out.println(e);
      }
    }

}

class SymCafeClientHandleConnection extends Thread {

    Socket ConnectSocket;
	java.awt.TextArea textArea1;
	java.awt.TextField textField1;

    SymCafeClientHandleConnection(Socket socket,
        TextArea myTextArea, TextField myTextField) {
        ConnectSocket=socket;
        textArea1=myTextArea;
        textField1=myTextField;
    }

    public void run() {

    try {

      // Get the input stream from the socket and convert
      // it into a data input stream
      DataInputStream Input=
	new DataInputStream(ConnectSocket.getInputStream());

      // Get the output stream from the socket and convert
      // it into a print stream
      PrintStream Output=
	new PrintStream(ConnectSocket.getOutputStream());

      // Send the server the command
      Output.println(textField1.getText());

      // The output buffer
      String line;

      // Read the results from the server until the socket closes
      while((line=Input.readLine())!=null) {
	     textArea1.appendText(line + "\n");
      }

	  textField1.setText("");
    } catch (IOException e) {
      System.out.println(e);
    }

  }
}
