import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
import java.awt.Event;
import java.awt.FlowLayout;
import java.awt.Label;
import java.awt.TextField;
import java.awt.TextArea;
import java.applet.Applet;

public class rantwall  extends  Applet {

// declare globally visible text area and field
TextArea  ta = null;
TextField  tf = null;

	public void init(){
		tf = new TextField("",40);
		ta = new TextArea("Type and <enter> something.",17,40);
		ta.setEditable(false);
		setLayout(new FlowLayout());
		add(new Label("Type here: "));
		add(tf);
		add(ta);
	}
	
	public boolean handleEvent(Event e){
		if(e.id == Event.KEY_PRESS){
			if(e.target == tf && e.key == 10) { 
				// enter key has been pressed
				if(tf.getText().length() > 0) 
					new postIt(this,tf.getText());
				tf.setText("");
			}
		}
		return false;
	}
}

class postIt {
  DataInputStream is = null;
  DataOutputStream os = null;

	public postIt(rantwall rwall, String str) {
		try{
			Socket sock = new Socket("newmedia.slc.edu", 80);
		        os =	new DataOutputStream(sock.getOutputStream());
			is =    new DataInputStream(sock.getInputStream());
			if(str != null) {
			   os.writeBytes("POST /cgi-bin/rantwall.cgi HTTP/1.0\n");
			   os.writeBytes("Content-type: text/plain\n");
			   os.writeBytes("Content-length: "+str.length()+"\n\n");
           		   for(int j=0;j<str.length();j++) 
					os.writeByte((byte)str.charAt(j));
			}
		// done sending new string
		// now, clear text area and read response from rantwall.cgi
			rwall.ta.setText("");
			String line = null;
                        while((line= is.readLine())!= null) {
                                 if(line.length()<2) break;  
                        }
		// end of HTTP header reached
                        while((line= is.readLine())!=null)
                        {
                                rwall.ta.appendText(line+'\n');
                        }                 
			sock.close();
		} catch (Exception e) { 	
			e.printStackTrace(); 
		}
	}
}
