This client is written as an applet instead of as an application. Thus, for reasons of WWW browser security, it may connect only to a server running on the same host the WWW server is running from which the HTML page was obtaianed that includes the applet tag for the client applet.
The primary difference between the appliction and the applet versions is that the applet version
extends Appletand does not include amainmethod. The entire program is shown, below. It may be excuted from a run applet anchor at the bottom.Complete Program
import java.applet.*; import java.awt.*; import java.awt.event.*; import java.net.*; import java.io.*; import java.util.*; public class jbsClientApplet extends Applet implements ActionListener, WindowListener { //***** Mulit-threaded client, sends and receives multiple messages // expected to connect to server on wwwj.cs.unc.edu:8902 protected String DEFAULT_HOST = "wwwj.cs.unc.edu"; protected int DEFAULT_PORT = 8902; String host; int port; jbsAppletConnectServer connection; Frame outerBox; TextField hostDisplay, portDisplay; TextArea logDisplay, sendDisplay, recvDisplay; Panel topPanel; Panel middlePanel; Panel buttonPanel; Button connectButton, sendButton, cancelButton, quitButton; // ************** init public void init () { buildUI (); } // end init //*********** Interface Methods *********** //**** ActionListener methods public void actionPerformed ( ActionEvent e ) { Object s = e.getSource(); // *** process Button actions if ( s instanceof Button ) { if ( s == connectButton ) { connection = new jbsAppletConnectServer ( this ); connection.start (); } // end connectButton if ( s == sendButton ) { connection.sendReceive (); } // end sendButton if ( s == cancelButton ) { sendDisplay.setText ( "" ); } // end cancelButton if ( s == quitButton ) { logDisplay.appendText ( "Closing connection and quitting\n" ); connection.closeConnection (); try {Thread.sleep ( 2000 );} catch ( InterruptedException except) {;} outerBox.hide (); outerBox.dispose (); System.exit ( 0 ); } // end quitButton } // end process Button actions } // end actionPerformed //**** WindowListener methods public void windowActivated ( WindowEvent e ) { } public void windowDeactivated ( WindowEvent e ) { } public void windowOpened ( WindowEvent e ) { } public void windowClosed ( WindowEvent e ) { } public void windowClosing ( WindowEvent e ) { outerBox.hide (); outerBox.dispose (); System.exit(0); } public void windowIconified ( WindowEvent e ) { } public void windowDeiconified ( WindowEvent e ) { } //*********** Utility Methods *********** // ************** buildUI private void buildUI () { String hostString, portString; hostString = getParameter ( "host" ); portString = getParameter ( "port" ); if ( hostString == null ) { hostDisplay = new TextField ( DEFAULT_HOST , 30 ); } else { hostDisplay = new TextField ( hostString, 30 ); } if ( portString == null ) { portDisplay = new TextField ( Integer.toString ( DEFAULT_PORT ), 30 ); } else { portDisplay = new TextField ( portString, 4 ); } topPanel = new Panel (); topPanel.setLayout ( new GridLayout ( 2, 1 ) ); topPanel.add ( hostDisplay ); topPanel.add ( portDisplay ); logDisplay = new TextArea ( 40, 10 ); sendDisplay = new TextArea ( 40, 5 ); recvDisplay = new TextArea ( 40, 5 ); sendDisplay.setText ("Default message."); recvDisplay.setText ("Messages received will be displayed here."); middlePanel = new Panel (); middlePanel.setLayout ( new GridLayout ( 3, 1 ) ); middlePanel.add ( logDisplay ); middlePanel.add ( sendDisplay ); middlePanel.add ( recvDisplay ); connectButton = new Button ( "Connect" ); sendButton = new Button ( "Send" ); cancelButton = new Button ( "Cancel" ); quitButton = new Button ( "Quit" ); connectButton.addActionListener ( this ); sendButton.addActionListener ( this ); cancelButton.addActionListener ( this ); quitButton.addActionListener ( this ); buttonPanel = new Panel ( ); buttonPanel.add ( connectButton ); buttonPanel.add ( sendButton ); buttonPanel.add ( cancelButton ); buttonPanel.add ( quitButton ); outerBox = new Frame ("Client_Applet"); outerBox.addWindowListener ( this ); outerBox.add ( "North", topPanel ); outerBox.add ( "Center", middlePanel ); outerBox.add ( "South", buttonPanel ); outerBox.resize ( 400, 450 ); outerBox.show (); } // end buildUI } // end jbsClientApplet// ********************************* ConnectServerclass jbsAppletConnectServer extends Thread { jbsClientApplet source; String host; int port; Socket connection; InputStream inStream; DataInputStream inDataStream; OutputStream outStream; DataOutputStream outDataStream; String message; // ************** jbsAppletConnectServer jbsAppletConnectServer ( jbsClientApplet c) { super (); source = (jbsClientApplet ) c; connectServer (); } // end constructor // ************** run public void run () { } // end run // ************** connectServer public void connectServer ( ) { host = source.hostDisplay.getText (); if ( host.equals ("" ) ) host = source.DEFAULT_HOST; if ( ! ( source.portDisplay.getText () ).equals ( "" ) ) port = Integer.parseInt ( source.portDisplay.getText () ); else port = source.DEFAULT_PORT; source.logDisplay.setText ( "Server specified: "+host+":"+port+"\n\n" ); try { connection = new Socket ( host, port ); outStream = connection.getOutputStream (); outDataStream = new DataOutputStream ( outStream ); inStream = connection.getInputStream (); inDataStream = new DataInputStream ( inStream ); source.logDisplay.appendText ( "Socket created: \n connecting to server "+host+":"+port+"\n\n" ); } // end try catch ( IOException except) { source.logDisplay.setText ( "Error connecting to server\n" ); except.printStackTrace (); System.exit ( 1 ); } // end catch } // end connectServer public void sendReceive () { try { message = source.sendDisplay.getText (); outDataStream.writeUTF ( message ); source.logDisplay.appendText ( "Message, below, sent to Server\n" ); source.recvDisplay.setText ( "" ); source.recvDisplay.setForeground ( Color.red ); message = inDataStream.readUTF (); source.recvDisplay.appendText ( message ); source.logDisplay.appendText ( " Message returned from server\n\n" ); } // end try for input catch ( EOFException except ) { source.logDisplay.appendText ( "EOF received\n" ); closeConnection (); } // end catch IOException catch ( IOException e ) { source.logDisplay.appendText ( "IOException\n" ); e.printStackTrace (); return; } // end catch IOException } // end sendReceive // ************** public void closeConnection () { try { connection.close (); } catch ( IOException except ) { source.logDisplay.appendText ( " Error clossing conncetion\n" ); except.printStackTrace (); } } // end closeConnection } // end jbsAppletConnectServer
Run the applet (expects server on wwwj.cs.unc.edu:8888)