/* File: EchoClient.java * * An applet to open a socket to the ThreadedEchoServer class. * This is set up to run on port 4882 and requires you to independently * run the Server on the web host. * */ import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; public class EchoClient extends java.applet.Applet implements ActionListener { TextArea t1, t2; Button send; Socket t; BufferedReader in; PrintWriter out; public void init() { t1 = new TextArea( "Type your message here.", 5, 40 ); t2 = new TextArea( "Echo Server replies will appear here.", 5, 40 ); send = new Button ( "Send >>>" ); send.addActionListener( this ); setLayout( new FlowLayout( FlowLayout.LEFT, 5,5 ) ); add( t1 ); add( send ); add( t2 ); try { t = new Socket( this.getCodeBase().getHost(), 4882 ); in = new BufferedReader( new InputStreamReader( t.getInputStream() ) ); out = new PrintWriter( t.getOutputStream(), true ); String str = in.readLine(); t2.setText( str ); } catch( Exception ex ) { System.out.println( ex ); t1.setText( ex.toString() ); } } public void actionPerformed ( ActionEvent e ) { try { out.println( t1.getText() ); t1.setText( "Sent" ); String str = in.readLine(); t2.setText( str ); } catch ( IOException ex ) { System.out.println( ex ); t1.setText( ex.toString() ); } } }