import java.awt.*;
import java.util.*;
import java.applet.*;
import com.roguewave.widgets.*;
// zpb_begin UserImports 
import java.net.*;
import java.io.*;
import java.util.*;
// zpb_end 

/**
 * Class JClient.
 *
 *
 * @author    Sam Pullara
 * @version   1.0 9/25/96 9:47:35am
 * @copyright Whittman-Hart, Inc.
 */
public
class JClient extends Applet {

	private static Applet applet;
	TextField pTextField;
	TextArea pTextArea;
	Button pSendButton;

	// Initial size in logical units
	Dimension initialSize = new Dimension(154, 99);
	// zpb_begin MainUserVars 
	// zpb_end 

	// zpb_begin MainJavadocAppletInit 
	/**
	 * Applet main entry point.  This method is automatically invoked by the
	 * Java interpreter in a web browser or the JDK appletviewer.
	 */
	 // zpb_end 
	public
	void init() {
		applet = this;

		// zpb_begin AppletInit 
		// zpb_end 

		setBackground(Color.lightGray);
		setFont(new Font("Helvetica", Font.PLAIN, 12));
		LogFontLayout lfLayout = new LogFontLayout(this);
		setLayout(lfLayout);

		pTextField = new TextField("");
		add("6 5 65 14", pTextField);
		pSendButton = new Button("Send");
		add("79 5 59 15", pSendButton);
		pTextArea = new TextArea("");
		add("6 28 130 51", pTextArea);
		pTextArea.setEditable(false);

		// zpb_begin MainConstructor_2 
		// zpb_end 

		// Size in logical units
		resize(getLayout().preferredLayoutSize(this));

		// zpb_begin MainConstructor_3 
		// zpb_end 
	}

	// zpb_begin MainJavadocMinimumSize 
	/**
	 * @return  Returns a Dimension with the minimum width and height
	 *          of this container.
	 */
	 // zpb_end 
	public Dimension minimumSize() {
		// zpb_begin MainMinimumSize 
		// zpb_end 
			
		LayoutManager layoutMgr = getLayout();
		if (layoutMgr instanceof LogFontLayout) {
			// Convert from logical units to absolute coordinates
			int w = initialSize.width;
			int h = initialSize.height;
			LogFontLayout layout = (LogFontLayout)layoutMgr;
			return new Dimension(layout.duX(w), layout.duY(h));
		}
		return new Dimension(initialSize);
	}

	// zpb_begin MainJavadocHandleEvent 
	/**
	 * Handle events that occur with components in this container.
	 *
	 * @return  Returns a boolean which specifies whether the event
	 *          handler should stop at this level or continue up the
	 *          parent hierarchy.
	 */
	// zpb_end 
	public boolean handleEvent(Event e) {

		// zpb_begin MainHandleEvent 
		// zpb_end 

		return super.handleEvent(e);
	}

	// zpb_begin MainJavadocGetFrame 
	/**
	 * Gets the frame for this container object.
	 *
	 * @return  Returns a Frame object.
	 */
	// zpb_end 
	public Frame getFrame(Container c) {
		// zpb_begin AppletGetFrame 
		// zpb_end 
		if (c instanceof Frame || c == null)
			return((Frame)c);
		  else
			return(getFrame(c.getParent()));
	}

	// zpb_begin MainJavadocAction 
	/**
	 * Action handling routine.
	 *
	 * @return  Returns a boolean specifying whether the event should be handled
	 *          furthur up the parent hierarchy.
	 */
	// zpb_end 
	public boolean action(Event evt, Object obj) {

		if (evt.target == pSendButton) {
			// zpb_begin MainSendButtonClicked 
			send();
			// zpb_end 
		}

		// zpb_begin MainAction 
		if (evt.target == pTextField) {
			send();
		} 
		// zpb_end 

		return false;
	}


	// zpb_begin MainAppletUserMethods 

	public void send() {
		JSpawnClient client=new JSpawnClient(pTextArea,pTextField);
		client.start();
	}

 
	// zpb_end 
}
// zpb_begin UserClasses 

class JSpawnClient extends Thread
{
 	java.awt.TextArea textArea1;
	java.awt.TextField textField1;

    JSpawnClient(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
	    JClientHandleConnection thread=
		  new JClientHandleConnection(ClientSocket,
			textArea1, textField1);
		thread.start();

      } catch (IOException e) {
		System.out.println(e);
      }
    }

}

class JClientHandleConnection extends Thread {

    Socket ConnectSocket;
	java.awt.TextArea textArea1;
	java.awt.TextField textField1;

    JClientHandleConnection(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);
    }

  }
}


// zpb_end 
