1 /* An applet to open a socket to the ThreadedEchoServer class. 2 * This is set up to run on port 4882 and requires you to independently 3 * run the Server on the web host. 4 * Nancy McCracken 5/16/97 5 */ 6 7 import java.awt.*; 8 import java.io.*; 9 import java.net.*; 10 11 public class EchoClient extends java.applet.Applet 12 { 13 TextField t1,t2; 14 Button send; 15 Socket t; 16 DataInputStream in; 17 PrintStream out; 18 19 public void init() 20 { 21 t1 = new TextField("Type your message here.", 40); 22 t2 = new TextField("Echo Server replies will appear here.",40); 23 send = new Button ("Send >>>"); 24 setLayout(new FlowLayout(FlowLayout.LEFT, 5,5)); 25 add(t1); 26 add(send); 27 add(t2); 28 29 try 30 { 31 t = new Socket(this.getCodeBase().getHost(), 4882); 32 in = new DataInputStream(t.getInputStream()); 33 out = new PrintStream(t.getOutputStream()); 34 35 String str = in.readLine(); 36 t2.setText(str); 37 } 38 catch(Exception ex) {}; 39 } 40 41 public boolean action (Event e, Object o) 42 { 43 if (e.target == send) 44 { 45 try 46 { 47 out.println(t1.getText()); 48 t1.setText(""); 49 String str = in.readLine(); 50 t2.setText(str); 51 } 52 catch (IOException ex) {}; 53 return true; 54 } 55 return false; 56 } 57 }