Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a simple applet. The program will use a label to hold the program Solutio

ID: 3644155 • Letter: C

Question

Create a simple applet. The program will use a label to hold the program

Explanation / Answer

import java.io.*; /* * This class defines the different type of messages that will be exchanged between the * Clients and the Server. * When talking from a Java Client to a Java Server a lot easier to pass Java objects, no * need to count bytes or to wait for a line feed at the end of the frame */ public class ChatMessage implements Serializable { protected static final long serialVersionUID = 1112122200L; // The different types of message sent by the Client // WHOISIN to receive the list of the users connected // MESSAGE an ordinary message // LOGOUT to disconnect from the Server static final int WHOISIN = 0, MESSAGE = 1, LOGOUT = 2; private int type; private String message; // constructor ChatMessage(int type, String message) { this.type = type; this.message = message; } // getters int getType() { return type; } String getMessage() { return message; } } Now the Server class. You can start the Server by typing > java Server at the console prompt. That will execute it in console mode and the server will wait for connection on port 1500. To use another port pass the port number to use as first parameter to the command > java Server 1200 will ask the Server to listen on port 1200. You can use C to stop the server. Server.java import java.io.*; import java.net.*; import java.text.SimpleDateFormat; import java.util.*; /* * The server that can be run both as a console application or a GUI */ public class Server { // a unique ID for each connection private static int uniqueId; // an ArrayList to keep the list of the Client private ArrayList al; // if I am in a GUI private ServerGUI sg; // to display time private SimpleDateFormat sdf; // the port number to listen for connection private int port; // the boolean that will be turned of to stop the /* 025 * server constructor that receive the port to listen to for connection as parameter 026 * in console 027 */ 028 public Server(int port) { 029 this(port, null); 030 } 031 032 public Server(int port, ServerGUI sg) { 033 // GUI or not 034 this.sg = sg; 035 // the port 036 this.port = port; 037 // to display hh:mm:ss 038 sdf = new SimpleDateFormat("HH:mm:ss"); 039 // ArrayList for the Client list 040 al = new ArrayList(); 041 } 042 043 public void start() { 044 keepGoing = true; 045 /* create socket server and wait for connection requests */ 046 try 047 { 048 // the socket used by the server 049 ServerSocket serverSocket = new ServerSocket(port); 050 051 // infinite loop to wait for connections 052 while(keepGoing) 053 { 054 // format message saying we are waiting 055 display("Server waiting for Clients on port " + port + "."); 056 057 Socket socket = serverSocket.accept(); // accept connection 058 // if I was asked to stop 059 if(!keepGoing) 060 break; 061 ClientThread t = new ClientThread(socket); // make a thread of it 062 al.add(t); // save it in the ArrayList 063 t.start(); 064 } 065 // I was asked to stop 066 try { 067 serverSocket.close(); 068 for(int i = 0; i = 0;) { 130 ClientThread ct = al.get(i); 131 // try to write to the Client if it fails remove it from the list 132 if(!ct.writeMsg(messageLf)) { 133 al.remove(i); 134 display("Disconnected Client " + ct.username + " removed from list."); 135 } 136 } 137 } 138 139 // for a client who logoff using the LOGOUT message 140 synchronized void remove(int id) { 141 // scan the array list until we found the Id 142 for(int i = 0; i java Server 155 * > java Server portNumber 156 * If the port number is not specified 1500 is used 157 */ 158 public static void main(String[] args) { 159 // start server on port 1500 unless a PortNumber is specified 160 int portNumber = 1500; 161 switch(args.length) { 162 case 1: 163 try { 164 portNumber = Integer.parseInt(args[0]); 165 } 166 catch(Exception e) { 167 System.out.println("Invalid port number."); 168 System.out.println("Usage is: > java Server [portNumber]"); 169 return; 170 } 171 case 0: 172 break; 173 default: 174 System.out.println("Usage is: > java Server [portNumber]"); 175 return; 176 177 } 178 // create a server object and start it 179 Server server = new Server(portNumber); 180 server.start(); 181 } 182 183 /** One instance of this thread will run for each client */ 184 class ClientThread extends Thread { 185 // the socket where to listen/talk 186 Socket socket; 187 ObjectInputStream sInput; 188 ObjectOutputStream sOutput; 189 // my unique id (easier for deconnection) 190 int id; 191 // the Username of the Client 192 String username; 193 // the only type of message a will receive 194 ChatMessage cm; 195 // the date I connect 196 String date; 197 198 // Constructore 199 ClientThread(Socket socket) { 200 // a unique id 201 id = ++uniqueId; 202 this.socket = socket; 203 /* Creating both Data Stream */ 204 System.out.println("Thread trying to create Object Input/Output Streams"); 205 try 206 { 207 // create output first 208 sOutput = new ObjectOutputStream(socket.getOutputStream()); 209 sInput = new ObjectInputStream(socket.getInputStream()); 210 // read the username 211 username = (String) sInput.readObject(); 212 display(username + " just connected."); 213 } 214 catch (IOException e) { 215 display("Exception creating new Input/output Streams: " + e); 216 return; 217 } 218 // have to catch ClassNotFoundException 219 // but I read a String, I am sure it will work 220 catch (ClassNotFoundException e) { 221 } 222 date = new Date().toString() + " "; 223 } 224 225 // what will run forever 226 public void run() { 227 // to loop until LOGOUT 228 boolean keepGoing = true; 229 while(keepGoing) { 230 // read a String (which is an object) 231 try { 232 cm = (ChatMessage) sInput.readObject(); 233 } 234 catch (IOException e) { 235 display(username + " Exception reading Streams: " + e); 236 break; 237 } 238 catch(ClassNotFoundException e2) { 239 break; 240 } 241 // the messaage part of the ChatMessage 242 String message = cm.getMessage(); 243 244 // Switch on the type of message receive 245 switch(cm.getType()) { 246 247 case ChatMessage.MESSAGE: 248 broadcast(username + ": " + message); 249 break; 250 case ChatMessage.LOGOUT: 251 display(username + " disconnected with a LOGOUT message."); 252 keepGoing = false; 253 break; 254 case ChatMessage.WHOISIN: 255 writeMsg("List of the users connected at " + sdf.format(new Date()) + " "); 256 // scan al the users connected 257 for(int i = 0; i
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote