
import java.awt.* ;
import java.awt.event.*;
import javax.swing.* ;

import java.io.* ;
import java.net.* ;

public class Telnet extends JFrame {  

	public static void main( String args[] ) {
		Telnet app = new Telnet() ;
		app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
	}

	JTextField   input ;
	JTextArea   output ;
	JButton      bexit ;

	Socket            s ;
	InputStreamReader   in ;
	OutputStreamWriter out ;

	public Telnet() {
		super("Telnet") ;

		input  = new JTextField(40) ;
		output = new JTextArea(25,80) ;
		bexit  = new JButton("Exit") ;

		JPanel botPanel = new JPanel() ;
		botPanel.setLayout( new FlowLayout() ) ;
		botPanel.add( input ) ;
		botPanel.add( bexit ) ;

		Container c = getContentPane() ;
		c.setLayout( new BorderLayout() ) ;
		c.add( output,  BorderLayout.CENTER ) ;
		c.add( botPanel, BorderLayout.SOUTH ) ;

		input.addActionListener( new GetInputHandler() ) ;
		bexit.addActionListener( new GetButtonHandler() ) ;

		setSize(600,450) ;
		setVisible(true) ;

		try {
			s   = new Socket( InetAddress.getByName( "virtual.dyc.edu" ), 4445 ) ;
			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( /*output.getText() + */ 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 ) {}
		}
	}

	private class GetButtonHandler implements ActionListener {
		public void actionPerformed( ActionEvent event ) {
			try {
				s.close() ;
				System.exit(0) ;
			} catch ( IOException exception ) {}
		}
	}
}


