
import java.awt.* ;
import java.awt.event.*;
import javax.swing.* ;

import java.io.* ;
import java.net.* ;

public class TelnetApplet extends JApplet {  

	JTextField   input ;
	JTextArea   output ;

	Socket               s ;
	InputStreamReader   in ;
	OutputStreamWriter out ;

	public void init() {

		input  = new JTextField(40) ;
		output = new JTextArea(25,80) ;

		Container c = getContentPane() ;
		c.setLayout( new BorderLayout() ) ;
		c.add( output,  BorderLayout.CENTER ) ;
		c.add(  input, BorderLayout.SOUTH ) ;

		input.addActionListener( new GetInputHandler() ) ;

		setSize(600,450) ;
		setVisible(true) ;

		try {
			//s   = new Socket( InetAddress.getByName( "virtual.dyc.edu" ), 4445 ) ;
			s   = new Socket( InetAddress.getByName( "127.0.0.1" ), 2323 ) ;
			in  = new InputStreamReader ( s.getInputStream () );
			out = new OutputStreamWriter( s.getOutputStream() );

			while ( true ) {
				String oul = "" ;
				if ( in.ready() ) {
					while ( in.ready() ) oul += (char) in.read() ;
					output.setText( oul ) ;
				}
			}
		} catch ( IOException exception ) {}
	}

	private class GetInputHandler implements ActionListener {
		public void actionPerformed( ActionEvent event ) {
			try {
				String inp = input.getText() + "\n" ;
				input.setText("") ;
				out.write(inp) ;
				out.flush() ;
				output.setText( output.getText() + inp ) ;
			} catch ( IOException exception ) {}
		}
	}
}


