// Test 1
   
import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;

public class Test1 extends Applet
{
	private Button readButton;
	private Button clearButton;
	private Label display;

	private Panel buttonPanel;
	private Panel displayPanel;

	public void init()
	{
		clearButton = new Button("Clear");
		readButton = new Button("Read");
		buttonPanel = new Panel();
		buttonPanel.add(clearButton);
		buttonPanel.add(readButton);

		display = new Label("The Display");
		displayPanel = new Panel();
		displayPanel.add(display);

		setLayout(new BorderLayout());
		add("North", buttonPanel);
		add("Center", displayPanel);
	}

	public boolean action(Event e, Object arg)
	{
		if (e.target == clearButton)
			display.setText("");
		else if (e.target == readButton)
			display.setText(readText());
		repaint();
		return true;
	}

	public String readText()
	{
		Socket s = null;
		String text = "";
		int textLength = 0;
		try
		{
			s = new Socket(getParameter("Server"), 80);
			DataInputStream in = new DataInputStream(s.getInputStream());
			DataOutputStream out = new DataOutputStream(s.getOutputStream());
			out.writeBytes("POST /cgi-bin/bprentice/Test1.pl HTTP/1.0\n");
			out.writeBytes("Content-type: text/plain\n");
			out.writeBytes("Content-length: 0\n\n");
			String line;
			do
			{
				line = in.readLine();
//				System.out.println(line);
				if (line.startsWith("Content-length:"))
					textLength = (new Integer(line.substring(16, line.length())).intValue());
			}
			while (! line.equals(""));
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < textLength; i++)
				sb.append((char) in.readByte());
			text = new String(sb);
//			System.out.println(text);
		}
		catch (Exception e1)
		{
			System.out.println("" + e1);
		}
		finally
		{
			try
			{
				if (s != null)
					s.close();
			}
			catch (Exception e2)
			{
			}
		}
		return text;
	}
}

