Chat System Java Project The main objective of developing this Chat System Java Project is to...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Chat System Java Project The main objective of developing this Chat System Java Project is to provide simple chat application between computer users. Here we are providing executed client chat window application. Find the below code and save it as ClientChatWindow.java in main Chat System Java Project folder. Chat System Java Project Source code Chat application is very powerful in internet. Chat System Java Project Source code provides total java code. Find the below added source code Chat Server. Creating a simple Chat Client/Server Solution Here is an example of how to extend a very simple client-server demo program into a fully functioning (but simple) Chat Client/Server package. There are five stages involved: Step 1: A simple server that will accept a single client connection and display everything the client says on the screen. If the client user types ".bye", the client and the server will both quit. Step 2: A server as before, but this time it will remain 'open' for additional connection once a client has quit. The server can handle at most one connection at a time. Step 3: A server as before, but this time it can handle multiple clients simultaneously. The output from all connected clients will appear on the server's screen. Step 4: A server as before, but this time it sends all text received from any of the connected clients to all clients. This means that the server has to receive and send, and the client has to send as well as receive Step 5: Wrapping the client from step 4 into a very simple GUI interface but not changing the functionality of either server or client. The client is implemented as an Applet, but a Frame would have worked just as well (for a stand-alone program). Step 1: Simple, one-time Server import java.net. *; import java.io.*; public class ChatServer { private Socket socket private Server Socket server private DataInputStream streamIn public Chat Server (int port) { try { ..."); wait } System.out.println("Binding to port " + port +", please server = new ServerSocket (port); System.out.println("Server started: " + server); System.out.println ("Waiting for a client ..."); socket server.accept (); System.out.println("Client accepted: + socket); open (); boolean done= false; while (!done) } { try { String line = streamIn.readUTF (); System.out.println (line); } null; null; = null; done = line.equals(".bye"); } catch (IOException ioe) { done = true; } close(); } catch (IOException ioe) { System.out.println (ioe); } } public void open () throws IOException { streamIn = new DataInputStream (new BufferedInputStream (socket.getInputStream ())); } public void close () throws IOException { if (socket != null) socket.close (); if (streamIn != null) streamIn.close(); else } public static void main(String args[]) { ChatServer server = null; if (args.length != 1) System.out.println ("Usage: java Chat Server port"); server = new ChatServer(Integer.parseInt (args [0] )); The Simple Client corresponding to the previous server (and to step 2 and step 3 servers as well): import java.net. *; import java.io.*; public class Chat Client { private Socket socket null; private DataInputStream console = null; private DataOutputStream streamOut = null; } public ChatClient (String serverName, int serverPort) { System.out.println("Establishing connection. Please wait..."); try { socket = new Socket (serverName, server Port); System.out.println ("Connected: " +socket); start (); } catch (UnknownHostException uhe) { System.out.println("Host unknown: " + uhe.getMessage()); } catch (IOException ioe) { System.out.println ("Unexpected exception: + ioe.getMessage()); } String line = ""; while (!line.equals(".bye")) { try { line = console.readLine (); streamOut.writeUTF (line); streamOut.flush(); } catch (IOException ioe) { System.out.println ("Sending error: + ioe.getMessage()); } } public void start () throws IOException { console = new DataInputStream (System.in); streamOut = new DataOutputStream (socket.getOutputStream()); } public void stop () { try { if (console != null) console.close(); if (streamOut != null) if (socket streamOut.close(); socket.close(); != null) } catch (IOException ioe) { System.out.println("Error closing ..."); } } public static void main(String args[]) { ChatClient client = null; if (args.length != 2) System.out.println("Usage: java ChatClient host port"); client = new ChatClient (args [0], Integer.parseInt (args [1])); else Step 2: Many Time Server, One Client (Client is as before) import java.net. *; import java.io.*; public class ChatServer implements Runnable socket = null; server = null; thread null; { private Socket private Server Socket private Thread. private DataInputStream streamIn = null; public Chat Server (int port) { try { System.out.println ("Binding to port + port + ", please ..."); wait server = new ServerSocket (port); System.out.println("Server started: + server) ; } catch (IOException ioe) { System.out.println (ioe); } } start (); } public void run () { while (thread != null) { } try { System.out.println("Waiting for a client ..."); socket server.accept (); System.out.println("Client accepted: " + socket); open (); boolean done= false; while (!done) { try { String line = streamIn. readUTF (); System.out.println (line); } done line.equals(".bye"); } catch (IOException ioe) { done true; } close(); } public void start (). { if (thread == null) 11 } catch (IOException ie) { System.out.println("Acceptance Error: { thread = new Thread (this); thread.start(); } public void stop () { if (thread != null) + ie); } { } thread.stop(); thread = null; } public void open () throws IOException { streamIn = new Data InputStream (new } public void close () throws IOException { if (socket != null) socket.close(); if (stream In != null) streamIn.close(); } public static void main (String args []) { ChatServer server = null; if (args.length != 1) System.out.println ("Usage: java ChatServer port"); BufferedInputStream (socket.getInputStream())); else wait server = new ChatServer (Integer.parseInt (args [0])); Step 3: Multi-Server handling Multi-Client (Client as before) import java.net. *; import java.io.*; public class ChatServer implements Runnable server null; thread null; { private Server Socket private Thread private ChatServerThread client = null; public Chat Server (int port) { try { System.out.println ("Binding to port + port + ", please. ..."); server = new ServerSocket (port); System.out.println ("Server started: start (); } catch (IOException ioe) { System.out.println(ioe); } } public void run () { while (thread != null) 11 addThread (server.accept()); + server); { try { System.out.println("Waiting for a client ..."); } catch (IOException ie) { System.out.println("Acceptance Error: + ie); } } public void addThread (Socket socket) { System.out.println("Client accepted: " + socket); client= new ChatServerThread (this, socket); } try ( client.open(); client.start (); } catch (IOException ioe) { System.out.println("Error opening thread: + ioe); } } public void start () { /* no change */ } public void stop() { /* no change */ } public static void main(String args[]) { /* no change */ } import java.net.*; import java.io. *; public class ChatServerThread extends Thread socket = null; private ChatServer server = null; ID = -1; private int private DataInputStream streamIn = null; { private Socket public Chat ServerThread (ChatServer server, Socket socket) { server = _server; socket = socket; ID socket.getPort (); } public void run () { System.out.println("Server Thread "+ID+ " running."); while (true) { try { System.out.println (streamIn.readUTF () ); } catch (IOException ioe) { } "1 } } public void open () throws IOException { streamIn = new DataInputStream (new BufferedInputStream (socket.getInputStream ())); } public void close () throws IOException { if (socket != null) socket.close(); if (stream In != null) streamIn.close(); Step 4: A Simple but functional, text based chat server/client import java.net. *; import java.io.*; public class ChatServer implements Runnable { private ChatServerThread clients[] = new ChatServerThread [50]; private Server Socket server = null; private Thread. thread = null; private int client Count = 0; public Chat Server (int port) { try { System.out.println ("Binding to port ..."); wait server = new Server Socket (port); System.out.println ("Server started: start(); } catch (IOException ioe). { ioe.getMessage()); } System.out.println("Can not bind to port } public void run () { while (thread != null) } public void start () public void stop () private int findClient (int ID) { for (int i = 0; i < client Count; i++) if (clients [i].getID () == ID) return i; return -1; { /* as before */ } { /* as before */ } + port + ", , please { try { System.out.println ("Waiting for a client ..."); addThread (server.accept()); } catch (IOException ioe) { System.out.println ("Server accept error: } + else for (int i = 0; i < clientCount; i++) clients [i].send (ID +": " + input); server) ; try { toTerminate.close(); } "1 ". + port + } public synchronized void handle (int ID, String input) { if (input.equals(".bye")) { clients [findClient (ID)].send(".bye"); remove (ID); } try { clients [clientCount]. open (); clients [clientCount].start (); } public synchronized void remove (int ID) { int pos findClient (ID); if (pos >= 0) { ChatServerThread toTerminate = clients [pos]; System.out.println ("Removing client thread " if (pos clientCount-1) for (int i = pos+1; i < clientCount; i++) clients [i-1] = clients [i]; clientCount--; + ID + +ioe); stop (); } "1 "1 "1 catch (IOException ioe) { System.out.println ("Error closing thread: + ioe); } toTerminate.stop(); } + at " } private void addThread (Socket socket) { if (client Count < clients.length) { System.out.println ("Client accepted: " + socket); clients [client Count] = new ChatServerThread (this, socket); + pos); { System.out.println("Error else System.out.println("Client } clientCount++; catch (IOException ioe) } import java.net. *; import java.io.*; reached."); } public static void main (String args []) { /* as before */ } public class ChatServerThread extends Thread { private ChatServer = null; private Socket = null; = -1; private int private DataInputStream streamIn = null; private DataOutputStream streamOut = null; public Chat ServerThread (ChatServer { super (); server = server; socket = socket; ID } } public void send (String msg) { try server socket ID } } = socket.getPort (); { streamOut.writeUTF (msg); streamOut.flush (); } public int getID () { return ID; } opening thread: + ioe); } } refused: maximum + clients.length + 11 } catch (IOException ioe) { System.out.println (ID + ERROR sending: " + ioe.getMessage ()); server.remove (ID); stop (); "1 public void run () { System.out.println("Server Thread " + ID + } server, Socket while (true) { try { server.handle (ID, stream In.readUTF () ); } 11 "1 socket) catch (IOException ioe) { System.out.println (ID + ERROR reading: + ioe.getMessage ()); server.remove (ID); stop (); running."); } public void open () throws IOException { streamIn = new DataInputStream (new Buffered InputStream (socket.getInput Stream ())); streamOut = new DataOutputStream (new BufferedOutputStream (socket.getOutputStream ())); public void close () throws IOException { if (socket != null) socket.close(); if (stream In != null) streamIn.close(); if (streamOut != null) streamOut.close(); } } import java.net. *; import java.io.*; public class ChatClient implements Runnable = { private Socket socket null; private Thread thread null; = null; private DataInputStream console private DataOutputStream streamOut = null; private ChatClientThread client = null; public Chat Client (String serverName, int serverPort) { System.out.println("Establishing connection. Please wait ..."); try { socket = new Socket (serverName, server Port); System.out.println("Connected: " + socket); start (); } catch (UnknownHostException uhe) { "1 System.out.println ("Host unknown: + uhe.getMessage()); } catch (IOException ioe) { System.out.println ("Unexpected exception: +ioe.getMessage ()); } } public void run () { while (thread != null) { try { streamOut.writeUTF (console.readLine () ); streamOut.flush(); } catch (IOException ioe) { System.out.println ("Sending error: + ioe.getMessage()); } } stop(); } public void handle (String msg) { if (msg.equals(".bye")) { System.out.println ("Good bye. Press RETURN to exit ..."); } else stop (); System.out.println (msg); } public void start () throws IOException "1 { console = new DataInputStream (System.in); streamOut = new DataOutputStream (socket.getOutputStream () ); if (thread == null) { client = new ChatClientThread (this, socket); thread=new Thread (this); thread.start (); } public void stop() { if (thread != null) { thread.stop(); thread= null; } try { if (console != null) if (streamOut != null) if (socket != null) } catch (IOException ioe) { System.out.println ("Error closing . . ."); } client.close(); client.stop(); } public static void main (String args []) { ChatClient client = null; if (args.length != 2) System.out.println ("Usage: java ChatClient host port"); client = new ChatClient (args [0], Integer.parseInt (args [1])); else } import java.net. *; import java.io. *; public class ChatClientThread extends Thread socket = null; { private Socket private ChatClient client. = null; private DataInputStream stream In = null; console.close(); streamOut.close(); socket.close(); public ChatClient Thread (ChatClient client, Socket socket) { client. = client; socket = __socket; open (); start (); } public void open () { try { streamIn = } catch (IOException ioe) { new Data InputStream (socket.getInputStream ()); client.stop(); " System.out.println("Error getting input stream: } public void close () { try { if (stream In != null) streamIn.close(); } catch (IOException ioe) { } } public void run () { while (true) System.out.println("Error closing input stream: + ioe); { try { client.handle (streamIn.readUTF ()); } + ioe); } catch (IOException ioe) { System.out.println("Listening error: + ioe.getMessage () ); } client.stop(); Stage 5: Chat client moved to very simple GUI interface import java.net. *; import java.io.*; import java.applet. *; import java.awt.*; public class ChatClient extends Applet send); { private Socket socket = null; private DataInputStream console = null; private DataOutputStream streamOut = null; private ChatClientThread client = null; private TextArea display = new TextArea (); private TextField input = new TextField(); private Button send = new Button ("Send"), connect = new Button ("Connect"), private String private int quit new Button ("Bye"); serverName = "localhost"; server Port = 4444; = "1 public void init () { Panel keys = new Panel(); keys.setLayout (new GridLayout (1,2)); keys.add (quit); keys.add (connect); Panel south = new Panel (); south.setLayout (new Border Layout ()); south.add("West", keys); south.add("Center", input); south.add("East", Label title = new Label ("Simple Chat Client Applet", Label.CENTER) ; title. setFont (new Font ("Helvetica", Font. BOLD, 14)); set Layout (new BorderLayout()); add ("North", title); add("Center", display); add ("South", south); quit.disable (); send.disable (); get Parameters (); } public boolean action (Event e, Object o) { if (e.target == quit) { input.setText (".bye"); send (); quit.disable (); send.disable(); connect.enable (); } else if (e. target == connect) { connect (serverName, serverPort); } else if (e.target == send) { send (); input.request Focus (); } return true; } public void connect (String serverName, int server Port) { println("Establishing connection. Please wait ..."); try { socket = new Socket (serverName, server Port); println("Connected: " + socket); open (); send.enable(); connect.disable (); quit.enable (); } catch (UnknownHostException uhe) { println ("Host unknown: + uhe.getMessage ()); } catch (IOException ioe) } {println ("Unexpected exception: " + ioe.getMessage()); } } private void send () { try { streamOut.writeUTF (input.getText ()); streamOut.flush(); input.setText (""); } catch (IOException ioe). "1 {println ("Sending error: " + ioe.getMessage () ); close (); } } public void handle (String msg) { if (msg.equals(".bye")) { println ("Good bye. Press RETURN to exit ..."); close (); } else println (msg); } public void open () { try { streamOut = new DataOutputStream (socket.getOutputStream () ); client = new ChatClientThread (this, socket); } catch (IOException ioe) { println ("Error opening output stream: + ioe); } } public void close() { try { if (streamOut != null) if (socket streamOut.close(); != null) socket.close(); } catch (IOException ioe) { println("Error closing ..."); } client.stop(); } client.close(); private void println (String msg) { display.appendText (msg + " "); } public void getParameters () { serverName = getParameter ("host"); serverPort = Integer. parseInt (get Parameter ("port")); } Chat System Java Project The main objective of developing this Chat System Java Project is to provide simple chat application between computer users. Here we are providing executed client chat window application. Find the below code and save it as ClientChatWindow.java in main Chat System Java Project folder. Chat System Java Project Source code Chat application is very powerful in internet. Chat System Java Project Source code provides total java code. Find the below added source code Chat Server. Creating a simple Chat Client/Server Solution Here is an example of how to extend a very simple client-server demo program into a fully functioning (but simple) Chat Client/Server package. There are five stages involved: Step 1: A simple server that will accept a single client connection and display everything the client says on the screen. If the client user types ".bye", the client and the server will both quit. Step 2: A server as before, but this time it will remain 'open' for additional connection once a client has quit. The server can handle at most one connection at a time. Step 3: A server as before, but this time it can handle multiple clients simultaneously. The output from all connected clients will appear on the server's screen. Step 4: A server as before, but this time it sends all text received from any of the connected clients to all clients. This means that the server has to receive and send, and the client has to send as well as receive Step 5: Wrapping the client from step 4 into a very simple GUI interface but not changing the functionality of either server or client. The client is implemented as an Applet, but a Frame would have worked just as well (for a stand-alone program). Step 1: Simple, one-time Server import java.net. *; import java.io.*; public class ChatServer { private Socket socket private Server Socket server private DataInputStream streamIn public Chat Server (int port) { try { ..."); wait } System.out.println("Binding to port " + port +", please server = new ServerSocket (port); System.out.println("Server started: " + server); System.out.println ("Waiting for a client ..."); socket server.accept (); System.out.println("Client accepted: + socket); open (); boolean done= false; while (!done) } { try { String line = streamIn.readUTF (); System.out.println (line); } null; null; = null; done = line.equals(".bye"); } catch (IOException ioe) { done = true; } close(); } catch (IOException ioe) { System.out.println (ioe); } } public void open () throws IOException { streamIn = new DataInputStream (new BufferedInputStream (socket.getInputStream ())); } public void close () throws IOException { if (socket != null) socket.close (); if (streamIn != null) streamIn.close(); else } public static void main(String args[]) { ChatServer server = null; if (args.length != 1) System.out.println ("Usage: java Chat Server port"); server = new ChatServer(Integer.parseInt (args [0] )); The Simple Client corresponding to the previous server (and to step 2 and step 3 servers as well): import java.net. *; import java.io.*; public class Chat Client { private Socket socket null; private DataInputStream console = null; private DataOutputStream streamOut = null; } public ChatClient (String serverName, int serverPort) { System.out.println("Establishing connection. Please wait..."); try { socket = new Socket (serverName, server Port); System.out.println ("Connected: " +socket); start (); } catch (UnknownHostException uhe) { System.out.println("Host unknown: " + uhe.getMessage()); } catch (IOException ioe) { System.out.println ("Unexpected exception: + ioe.getMessage()); } String line = ""; while (!line.equals(".bye")) { try { line = console.readLine (); streamOut.writeUTF (line); streamOut.flush(); } catch (IOException ioe) { System.out.println ("Sending error: + ioe.getMessage()); } } public void start () throws IOException { console = new DataInputStream (System.in); streamOut = new DataOutputStream (socket.getOutputStream()); } public void stop () { try { if (console != null) console.close(); if (streamOut != null) if (socket streamOut.close(); socket.close(); != null) } catch (IOException ioe) { System.out.println("Error closing ..."); } } public static void main(String args[]) { ChatClient client = null; if (args.length != 2) System.out.println("Usage: java ChatClient host port"); client = new ChatClient (args [0], Integer.parseInt (args [1])); else Step 2: Many Time Server, One Client (Client is as before) import java.net. *; import java.io.*; public class ChatServer implements Runnable socket = null; server = null; thread null; { private Socket private Server Socket private Thread. private DataInputStream streamIn = null; public Chat Server (int port) { try { System.out.println ("Binding to port + port + ", please ..."); wait server = new ServerSocket (port); System.out.println("Server started: + server) ; } catch (IOException ioe) { System.out.println (ioe); } } start (); } public void run () { while (thread != null) { } try { System.out.println("Waiting for a client ..."); socket server.accept (); System.out.println("Client accepted: " + socket); open (); boolean done= false; while (!done) { try { String line = streamIn. readUTF (); System.out.println (line); } done line.equals(".bye"); } catch (IOException ioe) { done true; } close(); } public void start (). { if (thread == null) 11 } catch (IOException ie) { System.out.println("Acceptance Error: { thread = new Thread (this); thread.start(); } public void stop () { if (thread != null) + ie); } { } thread.stop(); thread = null; } public void open () throws IOException { streamIn = new Data InputStream (new } public void close () throws IOException { if (socket != null) socket.close(); if (stream In != null) streamIn.close(); } public static void main (String args []) { ChatServer server = null; if (args.length != 1) System.out.println ("Usage: java ChatServer port"); BufferedInputStream (socket.getInputStream())); else wait server = new ChatServer (Integer.parseInt (args [0])); Step 3: Multi-Server handling Multi-Client (Client as before) import java.net. *; import java.io.*; public class ChatServer implements Runnable server null; thread null; { private Server Socket private Thread private ChatServerThread client = null; public Chat Server (int port) { try { System.out.println ("Binding to port + port + ", please. ..."); server = new ServerSocket (port); System.out.println ("Server started: start (); } catch (IOException ioe) { System.out.println(ioe); } } public void run () { while (thread != null) 11 addThread (server.accept()); + server); { try { System.out.println("Waiting for a client ..."); } catch (IOException ie) { System.out.println("Acceptance Error: + ie); } } public void addThread (Socket socket) { System.out.println("Client accepted: " + socket); client= new ChatServerThread (this, socket); } try ( client.open(); client.start (); } catch (IOException ioe) { System.out.println("Error opening thread: + ioe); } } public void start () { /* no change */ } public void stop() { /* no change */ } public static void main(String args[]) { /* no change */ } import java.net.*; import java.io. *; public class ChatServerThread extends Thread socket = null; private ChatServer server = null; ID = -1; private int private DataInputStream streamIn = null; { private Socket public Chat ServerThread (ChatServer server, Socket socket) { server = _server; socket = socket; ID socket.getPort (); } public void run () { System.out.println("Server Thread "+ID+ " running."); while (true) { try { System.out.println (streamIn.readUTF () ); } catch (IOException ioe) { } "1 } } public void open () throws IOException { streamIn = new DataInputStream (new BufferedInputStream (socket.getInputStream ())); } public void close () throws IOException { if (socket != null) socket.close(); if (stream In != null) streamIn.close(); Step 4: A Simple but functional, text based chat server/client import java.net. *; import java.io.*; public class ChatServer implements Runnable { private ChatServerThread clients[] = new ChatServerThread [50]; private Server Socket server = null; private Thread. thread = null; private int client Count = 0; public Chat Server (int port) { try { System.out.println ("Binding to port ..."); wait server = new Server Socket (port); System.out.println ("Server started: start(); } catch (IOException ioe). { ioe.getMessage()); } System.out.println("Can not bind to port } public void run () { while (thread != null) } public void start () public void stop () private int findClient (int ID) { for (int i = 0; i < client Count; i++) if (clients [i].getID () == ID) return i; return -1; { /* as before */ } { /* as before */ } + port + ", , please { try { System.out.println ("Waiting for a client ..."); addThread (server.accept()); } catch (IOException ioe) { System.out.println ("Server accept error: } + else for (int i = 0; i < clientCount; i++) clients [i].send (ID +": " + input); server) ; try { toTerminate.close(); } "1 ". + port + } public synchronized void handle (int ID, String input) { if (input.equals(".bye")) { clients [findClient (ID)].send(".bye"); remove (ID); } try { clients [clientCount]. open (); clients [clientCount].start (); } public synchronized void remove (int ID) { int pos findClient (ID); if (pos >= 0) { ChatServerThread toTerminate = clients [pos]; System.out.println ("Removing client thread " if (pos clientCount-1) for (int i = pos+1; i < clientCount; i++) clients [i-1] = clients [i]; clientCount--; + ID + +ioe); stop (); } "1 "1 "1 catch (IOException ioe) { System.out.println ("Error closing thread: + ioe); } toTerminate.stop(); } + at " } private void addThread (Socket socket) { if (client Count < clients.length) { System.out.println ("Client accepted: " + socket); clients [client Count] = new ChatServerThread (this, socket); + pos); { System.out.println("Error else System.out.println("Client } clientCount++; catch (IOException ioe) } import java.net. *; import java.io.*; reached."); } public static void main (String args []) { /* as before */ } public class ChatServerThread extends Thread { private ChatServer = null; private Socket = null; = -1; private int private DataInputStream streamIn = null; private DataOutputStream streamOut = null; public Chat ServerThread (ChatServer { super (); server = server; socket = socket; ID } } public void send (String msg) { try server socket ID } } = socket.getPort (); { streamOut.writeUTF (msg); streamOut.flush (); } public int getID () { return ID; } opening thread: + ioe); } } refused: maximum + clients.length + 11 } catch (IOException ioe) { System.out.println (ID + ERROR sending: " + ioe.getMessage ()); server.remove (ID); stop (); "1 public void run () { System.out.println("Server Thread " + ID + } server, Socket while (true) { try { server.handle (ID, stream In.readUTF () ); } 11 "1 socket) catch (IOException ioe) { System.out.println (ID + ERROR reading: + ioe.getMessage ()); server.remove (ID); stop (); running."); } public void open () throws IOException { streamIn = new DataInputStream (new Buffered InputStream (socket.getInput Stream ())); streamOut = new DataOutputStream (new BufferedOutputStream (socket.getOutputStream ())); public void close () throws IOException { if (socket != null) socket.close(); if (stream In != null) streamIn.close(); if (streamOut != null) streamOut.close(); } } import java.net. *; import java.io.*; public class ChatClient implements Runnable = { private Socket socket null; private Thread thread null; = null; private DataInputStream console private DataOutputStream streamOut = null; private ChatClientThread client = null; public Chat Client (String serverName, int serverPort) { System.out.println("Establishing connection. Please wait ..."); try { socket = new Socket (serverName, server Port); System.out.println("Connected: " + socket); start (); } catch (UnknownHostException uhe) { "1 System.out.println ("Host unknown: + uhe.getMessage()); } catch (IOException ioe) { System.out.println ("Unexpected exception: +ioe.getMessage ()); } } public void run () { while (thread != null) { try { streamOut.writeUTF (console.readLine () ); streamOut.flush(); } catch (IOException ioe) { System.out.println ("Sending error: + ioe.getMessage()); } } stop(); } public void handle (String msg) { if (msg.equals(".bye")) { System.out.println ("Good bye. Press RETURN to exit ..."); } else stop (); System.out.println (msg); } public void start () throws IOException "1 { console = new DataInputStream (System.in); streamOut = new DataOutputStream (socket.getOutputStream () ); if (thread == null) { client = new ChatClientThread (this, socket); thread=new Thread (this); thread.start (); } public void stop() { if (thread != null) { thread.stop(); thread= null; } try { if (console != null) if (streamOut != null) if (socket != null) } catch (IOException ioe) { System.out.println ("Error closing . . ."); } client.close(); client.stop(); } public static void main (String args []) { ChatClient client = null; if (args.length != 2) System.out.println ("Usage: java ChatClient host port"); client = new ChatClient (args [0], Integer.parseInt (args [1])); else } import java.net. *; import java.io. *; public class ChatClientThread extends Thread socket = null; { private Socket private ChatClient client. = null; private DataInputStream stream In = null; console.close(); streamOut.close(); socket.close(); public ChatClient Thread (ChatClient client, Socket socket) { client. = client; socket = __socket; open (); start (); } public void open () { try { streamIn = } catch (IOException ioe) { new Data InputStream (socket.getInputStream ()); client.stop(); " System.out.println("Error getting input stream: } public void close () { try { if (stream In != null) streamIn.close(); } catch (IOException ioe) { } } public void run () { while (true) System.out.println("Error closing input stream: + ioe); { try { client.handle (streamIn.readUTF ()); } + ioe); } catch (IOException ioe) { System.out.println("Listening error: + ioe.getMessage () ); } client.stop(); Stage 5: Chat client moved to very simple GUI interface import java.net. *; import java.io.*; import java.applet. *; import java.awt.*; public class ChatClient extends Applet send); { private Socket socket = null; private DataInputStream console = null; private DataOutputStream streamOut = null; private ChatClientThread client = null; private TextArea display = new TextArea (); private TextField input = new TextField(); private Button send = new Button ("Send"), connect = new Button ("Connect"), private String private int quit new Button ("Bye"); serverName = "localhost"; server Port = 4444; = "1 public void init () { Panel keys = new Panel(); keys.setLayout (new GridLayout (1,2)); keys.add (quit); keys.add (connect); Panel south = new Panel (); south.setLayout (new Border Layout ()); south.add("West", keys); south.add("Center", input); south.add("East", Label title = new Label ("Simple Chat Client Applet", Label.CENTER) ; title. setFont (new Font ("Helvetica", Font. BOLD, 14)); set Layout (new BorderLayout()); add ("North", title); add("Center", display); add ("South", south); quit.disable (); send.disable (); get Parameters (); } public boolean action (Event e, Object o) { if (e.target == quit) { input.setText (".bye"); send (); quit.disable (); send.disable(); connect.enable (); } else if (e. target == connect) { connect (serverName, serverPort); } else if (e.target == send) { send (); input.request Focus (); } return true; } public void connect (String serverName, int server Port) { println("Establishing connection. Please wait ..."); try { socket = new Socket (serverName, server Port); println("Connected: " + socket); open (); send.enable(); connect.disable (); quit.enable (); } catch (UnknownHostException uhe) { println ("Host unknown: + uhe.getMessage ()); } catch (IOException ioe) } {println ("Unexpected exception: " + ioe.getMessage()); } } private void send () { try { streamOut.writeUTF (input.getText ()); streamOut.flush(); input.setText (""); } catch (IOException ioe). "1 {println ("Sending error: " + ioe.getMessage () ); close (); } } public void handle (String msg) { if (msg.equals(".bye")) { println ("Good bye. Press RETURN to exit ..."); close (); } else println (msg); } public void open () { try { streamOut = new DataOutputStream (socket.getOutputStream () ); client = new ChatClientThread (this, socket); } catch (IOException ioe) { println ("Error opening output stream: + ioe); } } public void close() { try { if (streamOut != null) if (socket streamOut.close(); != null) socket.close(); } catch (IOException ioe) { println("Error closing ..."); } client.stop(); } client.close(); private void println (String msg) { display.appendText (msg + " "); } public void getParameters () { serverName = getParameter ("host"); serverPort = Integer. parseInt (get Parameter ("port")); }
Expert Answer:
Related Book For
Government and Not for Profit Accounting Concepts and Practices
ISBN: 978-1118155974
6th edition
Authors: Michael H. Granof, Saleha B. Khumawala
Posted Date:
Students also viewed these computer network questions
-
Planning is one of the most important management functions in any business. A front office managers first step in planning should involve determine the departments goals. Planning also includes...
-
Design a Java class that represents a cache with a fixed size. It should support operations like add, retrieve, and remove, and it should evict the least recently used item when it reaches capacity.
-
1. Internal controls are concerned with A) only manual systems of accounting. B) the extent of government regulations. safeguarding assets. C) D) preparing income tax returns. 2. Having one person...
-
A ball is thrown vertically upward, which is the positive direction. A little later it returns to its point of release. The ball is in the air for a total time of 8.0 s. What is its initial velocity?...
-
Christopher Bentley purchased a 10-year-old Saab with 130,000 miles on its odometer for $2,000. His state mandates that he have automobile insurance for the vehicle. The cost of insurance may be more...
-
A parallel-plate capacitor with air between its plates carries a charge of \(6.60 \mu \mathrm{C}\) when a \(9.00-\mathrm{V}\) battery is connected to it. How much energy is stored in the capacitor?
-
A firm has the following items on its balance sheet: Cash ........................$ 20,000,000 Inventory ...................... 134,000,000 Notes payable to bank ................ 31,500,000 Common...
-
1. Identify the product which represents the solid state in the above reaction. a) Barium chloride b) Barium sulphate c) Sodium chloride d) Sodium sulphate 2. The colour of the solution observed...
-
Tonys Precision Computer Centre is picking up in business, so he has decided to expand his bookkeeping system to a general journal/ledger system. The balances from June have been forwarded to the...
-
A few years ago a couple purchased an office space by financing RA for n years, paying periodic installment of Rp with an interest of r% compounded bimonthly (every 2 months). They have made t...
-
Explain the conditions for implementing cooperation in the use of state/regional goods by the provisions of Government Regulation 6 of 2006 & Government Regulation 38 of 2008.
-
Identify the audience and purpose for an effective business proposal. How will the audience be convinced to implement this proposal? Consider the following: The language choices of the proposal The...
-
What is the psychological definition of personality? Discuss how nature (biology) and nurture (culture, religion, upbringing, etc.) have contributed to the uniqueness and development of your...
-
What would the two process model of sleep look like for an individual with damage to their suprachiasmatic nucleus ?
-
EnergyAustralia is one of Australia's residential electricity retail companies. EnergyAustralia sources 30% of its energy from coal seam gas supplies. 2% from hydroelectric power sources. 2% from...
-
The quality control department employs five technicians during the day shift. Listed below are the numbers of times the technicians instructed the production foreman to shut down the manufacturing...
-
Define cultural intelligence. Cite the books or journal articles you found in Capella's library. Explain why cultural intelligence is important for HR practitioners and other organizational managers.
-
Assume that you are a loan ofcer of a bank. A local church is seeking a $4 million, 20-year loan to construct a new classroom building. Church ofcers submit a comprehensive nancial report that was...
-
In January 2015, Kirkland University receives a pledge of $200,000, to be used exclusively to support research in a specialized area of communication disorders. The universitys scal year ends on July...
-
1. Which of the following is true with respect to bankruptcy? a. Per the federal bankruptcy code, a municipality can be declared bankrupt but not insolvent. b. Several major cities have avoided...
-
In a boiler trial observations made are as: Feed water temperature \(=30^{\circ} \mathrm{C}\) Boiler pressure \(\quad=12 \mathrm{bar}\) Dryness fraction of steam \(=0.80\) Coal consumption...
-
How much heat is to be added to convert \(4 \mathrm{~kg}\) of water at \(20^{\circ} \mathrm{C}\) in to steam at 8 bar and \(200^{\circ} \mathrm{C}\). Take \(C_{p}\) of superheated steam as \(2.1...
-
What do you understand by mechanical and thermal efficiency? A steam plant uses 3 tonne of \(\mathrm{coal} / \mathrm{h}\). The steam is fed to turbine the output of which is \(4 \mathrm{MW}\). The...
Study smarter with the SolutionInn App