1 /* File: EchoClient.java 2 * 3 * An applet to open a socket to the ThreadedEchoServer class. 4 * This is set up to run on port 4882 and requires you to independently 5 * run the Server on the web host. 6 * 7 */ 8 9 import java.awt.*; 10 import java.awt.event.*; 11 import java.io.*; 12 import java.net.*; 13 14 public class EchoClient extends java.applet.Applet 15 implements ActionListener 16 { 17 TextArea t1, t2; 18 Button send; 19 Socket t; 20 BufferedReader in; 21 PrintWriter out; 22 23 public void init() 24 { 25 t1 = new TextArea( "Type your message here.", 5, 40 ); 26 t2 = new TextArea( "Echo Server replies will appear here.", 5, 40 ); 27 send = new Button ( "Send >>>" ); 28 send.addActionListener( this ); 29 setLayout( new FlowLayout( FlowLayout.LEFT, 5,5 ) ); 30 add( t1 ); 31 add( send ); 32 add( t2 ); 33 34 try 35 { 36 t = new Socket( this.getCodeBase().getHost(), 4882 ); 37 in = new BufferedReader( 38 new InputStreamReader( t.getInputStream() ) ); 39 out = new PrintWriter( t.getOutputStream(), true ); 40 41 String str = in.readLine(); 42 t2.setText( str ); 43 } 44 catch( Exception ex ) 45 { System.out.println( ex ); 46 t1.setText( ex.toString() ); 47 } 48 } 49 50 public void actionPerformed ( ActionEvent e ) 51 { 52 try 53 { 54 out.println( t1.getText() ); 55 t1.setText( "Sent" ); 56 String str = in.readLine(); 57 t2.setText( str ); 58 } 59 catch ( IOException ex ) 60 { System.out.println( ex ); 61 t1.setText( ex.toString() ); 62 } 63 } 64 }