import java.applet.Applet; import java.awt.*; import java.net.*; import java.io.*; /** * Demonstrate a talk server * Open a TCP socket to the specified server * */ public class Talk extends Applet implements Runnable { String DestinationServerName = new String("traveler"); int PortNumber = 8000; Get get; Put put; boolean debug = true; TextField sendbox = new TextField(); TextArea listenbox = new TextArea(); Button sendbutton = new Button("SEND"); Frame f = null; ///////////////////////////////////////////////// // Start listening on the requested port ///////////////////////////////////////////////// public void init() { if ( debug) System.out.println("Start get Thread"); listenbox.setEditable(false); get = new Get(this,debug); if ( debug) System.out.println("Initialize put"); put = new Put(this,debug); } public Talk( Frame f ) { this.f = f; } public void start() { get.start(); try { Thread.sleep(1000); } catch ( InterruptedException e ) { } } ///////////////////////////////////////////////// // Send the message the user has entered ///////////////////////////////////////////////// public boolean action(Event evt, Object arg) { if ( debug ) System.out.println("action routine" + arg + evt); if ("SEND".equals(arg)) { String message = new String(sendbox.getText()); if ( debug ) { System.out.println("send button pressed"); System.out.println("Send > " + message); } put.sendit(message); sendbox.setText(null); } return ( true ); } public void run() { } public static void main(String args[]) { if (args.length!=2) { System.out.println("java Java-Talk "); return; } Frame f = new Popup("Java Talk"); Talk talk = new Talk(f); talk.PortNumber = Integer.parseInt(args[1]); talk.DestinationServerName = new String(args[0]); talk.init(); talk.start(); talk.setLayout(new BorderLayout()); talk.add("North",talk.sendbox); talk.add("Center",talk.listenbox); talk.add("South",talk.sendbutton); f.add("Center", talk); f.resize(300, 300); f.show(); } } /** * Popup a frame for the user input and responses from other system */ class Popup extends Frame { String title; Popup (String title) { super(); this.title = title; setTitle(title); MenuBar mb = new MenuBar(); Menu m = new Menu("File"); m.add(new MenuItem("Quit")); mb.add(m); setMenuBar(mb); } /** * Process action events with this */ public boolean action(Event e, Object arg) { if (e.target instanceof MenuItem) { Menu menu = (Menu)(((MenuItem)e.target).getParent()); String label = (String)arg; if (label.equals("Quit")) { dispose(); System.exit(0); } } return ( true); } } /** * Get a message from the Sender */ class Get extends Thread { Talk app; boolean debug; public Get(Talk app,boolean debug) { this.app = app; this.debug = debug; } public void run() { byte b[] = new byte [10]; ServerSocket s; Socket rs; InputStream is; String response; int nbytes = 0; if ( debug) System.out.println("Get Thread Running"); //////////////////////////////////////////////////// // Open a socket to the specified recipient //////////////////////////////////////////////////// try { s = new ServerSocket(app.PortNumber); rs = s.accept(); is = rs.getInputStream(); } catch(Exception e) { System.out.println("Unable to talk to server " + app.DestinationServerName + " at port " + app.PortNumber); return; } /////////////////////////////////////////////////////////////////////////////// // Read continuously and post the message to the text area as it arrives /////////////////////////////////////////////////////////////////////////////// while ( true ) { if ( debug ) System.out.println("Looking for message"); try { nbytes = is.read(b,0,10); } catch ( IOException e ) { } if ( nbytes > 0 ) { if ( debug ) System.out.println("Result of read is " + nbytes); response = new String(b,0,0,nbytes); app.listenbox.appendText(response); app.listenbox.appendText(new String("\n")); } else { if ( debug ) System.out.println("Shut down get"); return; } } } } /** * Send a message to the recipient */ class Put { Talk app; Socket s = null; OutputStream os = null; boolean debug; public Put(Talk app, boolean debug) { this.app = app; this.debug = debug; } //////////////////////////////////////// // Send the requested string //////////////////////////////////////// public void sendit(String message) { int mlength = message.length(); byte b[] = new byte [message.length()]; if ( debug ) System.out.println("Put Running"); try { if ( s == null ) { s = new Socket(app.DestinationServerName,app.PortNumber); os = s.getOutputStream(); } } catch(Exception e) { System.out.println("Unable to talk to server " + app.DestinationServerName + " at port " + app.PortNumber); return; } message.getBytes(0,mlength,b,0); try { System.out.println("Sending message"); os.write(b,0,mlength); } catch(Exception e) { System.out.println("Unable to talk to server " + app.DestinationServerName + " at port " + app.PortNumber); } } }