// A simple Java chat client // David Powell 8/8/97 // Department of Computer Science, Monash University, Australia 3168 import java.applet.*; import java.awt.*; import java.lang.*; import java.net.*; import java.io.*; import java.util.*; public class Client extends Applet implements Runnable { TextArea output; TextField input; Button connect; Button disconn; Connection c; Thread thread; String host; int port; public void init() { host = getParameter("host"); port = (new Integer(getParameter("port"))).intValue(); setLayout(new BorderLayout()); // Input area input = new TextField(60); // Output area output = new TextArea(20, 60); output.setEditable(false); // Buttons to connect and disconnect from the server connect = new Button("Connect"); disconn = new Button("Disconnect"); add("North",input); add("Center",output); Panel buttons = new Panel(); buttons.add(connect); buttons.add(disconn); add("South", buttons); // Create a new connection class, but don't actually connect to the server. c = new Connection(); } public void start() { thread = new Thread(this); thread.start(); } public void stop() { thread.stop(); thread = null; } public void disconn() { if (!c.connected()) { output.appendText("Not connected.\n"); } else { c.close(); output.appendText("Disconnected.\n"); stop(); } } public void connect() { if (c.connected()) { output.appendText("Already connected!\n"); return; } // Try to connect to the server output.appendText("Attempting to connect...\n"); c.connect(host, port); if (c.connected()) { output.appendText("Connection established.\n"); start(); } else { output.appendText("Unable to connect.\n"); } } // Allow it to be run as an application. public static void main(String args[]) { Frame f = new Frame("Client"); Client client = new Client(); client.init(); f.add("Center", client); f.pack(); f.show(); } // run() is used to read the socket and print anything sent to us. // Note: this routine uses `canRead' so it will not block in the read(), // (not necessary for this simple example. ie. blocking would be ok.) public void run() { while(c.connected()) { if (c.canRead()) { // If there is data pending on the connection, String s = c.read(); // print it out. if (s.length() > 0) output.appendText("READ: "+s); } try { thread.sleep(100); } // Pause briefly. catch (Exception e) { } } } // Handle any button clicks, or an enter line of text. public boolean handleEvent(Event e) { switch(e.id) { case Event.ACTION_EVENT: // Strings are read a line at a time and if (e.target == input) { // written to the connection. if (c.connected()) { c.write((String)e.arg); } else { output.appendText("Not connected.\n"); } input.setText(""); return true; } else if (e.target == connect) { connect(); } else if (e.target == disconn) { disconn(); } } return false; } }//Client // ---------------------------------------------------------------------------- // class Connection handles low level reads and writes to a socket. // A Connection can be 'connected', can be 'read' from and 'write' to. // 'canRead' is true is there is data to be 'read'. class Connection { final int MAXLENGTH = 1024; // Maximum length to be read at a time Socket socket; PrintStream out; DataInputStream in; Connection() { } public boolean connected() { return (socket!=null); } public boolean connect(String h, int p) { try { socket = new Socket(h, p); } catch (Exception e) { System.err.println("Socket open: "+e); } if (socket==null) return false; try { in = new DataInputStream(socket.getInputStream()); out = new PrintStream(socket.getOutputStream()); } catch (Exception e) { System.err.println("Socket getInput: "+e); } return true; } public void close() { try { socket.close(); } catch (Exception e) { System.err.println("Socket close: "+e); } socket=null; } // Send data to the socket public void write(String s) { out.println(s); } // Is there data available? public boolean canRead() { try { return in.available()!=0; } catch (Exception e) { System.err.println("Socket available: "+e); socket=null;} return false; } // Read some data. Will block if nothing available. public String read() { String s; byte b[] = new byte[MAXLENGTH]; int num = 0; try { num = in.read(b, 0, MAXLENGTH); } catch (Exception e) { System.err.println("Socket read: "+e); socket=null;} if (num>0) s = new String(b, 0, 0, num); else s = ""; return s; } }//Connection // David Powell 8/8/1997 // Department of Computer Science, Monash University, Australia 3168