Dear, I have a ChatServer.java files as below. It is a round robin chating system. There are two client chatting. Now I want to modify below file that allow the discussion to keep going until one of the user input is "BYE" in uppercase without any preceding and trailing characters. After one user input of "BYE" will terminates everyone else including the server. How do I modifty? Please help! I know the "for ( int i=0; i < 3; i++)"statemnet need to modifty. How to do it? -------------ChatServer.java-------------------- import java.io.*; import java.net.*;
public class ChatServer.java { // Valid PORT_NUMBER ranges from 1 to 65,535. However the numbers from 1 to 1023 // are reserved, for example, 80 is reserved for http. private final static int PORT_NUMBER = 3030;
public static void main (String[] args) throws IOException { Socket socket1, socket2; DataInputStream dataIn1, dataIn2; DataOutputStream dataOut1, dataOut2; String line;
System.out.println ( "\r\n-- Server Started --\r\n" );
// We need to create a ServerSocket at a free port number. ServerSocket server = new ServerSocket ( PORT_NUMBER );
// Calling the accept method, the server waits for a client to make a TCP connection. socket1 = server.accept (); socket2 = server.accept ();
// ServerSocket is only needed to handle connection request. Once the connection // is made, the ServerSocket can be closed even the sockets remain open. // Normally, the ServerSocket won't be closed this early because real applications // are designed to handle arbitrarily many clients. However we only handle two. server.close ();
try { // There should be a pair of input and output streams for each socket. // The output stream of the server corresponds to the input stream of the client. // Therefore if we send a line on the output stream, the client should read // the line in the corresponding input stream. dataIn1 = new DataInputStream (new BufferedInputStream (socket1.getInputStream ())); dataOut1 = new DataOutputStream (new BufferedOutputStream (socket1.getOutputStream ()));
dataIn2 = new DataInputStream (new BufferedInputStream (socket2.getInputStream ())); dataOut2 = new DataOutputStream (new BufferedOutputStream (socket2.getOutputStream ()));
for ( int i=0; i < 3; i++) { // Read a line from the client 1 line = dataIn1.readUTF ( ); // Write a line to client 2 dataOut2.writeUTF ( line ); dataOut2.flush ( ); // Show the line on the server's screen System.out.println ( "\r\nClient 1: " + line); System.out.flush( );
// Read a line from the client 2 line = dataIn2.readUTF ( ); // Write a line to client 1 dataOut1.writeUTF ( line ); dataOut1.flush ( ); // Show the line on the server's screen System.out.println ( "Client 2: " + line); System.out.flush( ); } } catch (IOException ex) { throw ex; } finally { System.out.println ("\r\n-- Shutdown --"); socket1.close (); socket2.close (); } } } ---------------ChatClient1.java as below----------------- import java.io.*; import java.net.*;
public class ChatClient1 { // Use an IP address or localhost for host private final static String HOST = "localhost"; // The PORT_NUMBER here must match the server's PORT_NUMBER. private final static int PORT_NUMBER = 3030;
public static void main (String[] args) throws IOException { DataInputStream dataIn; DataOutputStream dataOut; String line;
Reader kbd = new FileReader (FileDescriptor.in); BufferedReader bufferedKbd = new BufferedReader (kbd);
System.out.println ( "\r\n-- Client 1 Started --\r\n" );
// Request a TCP connection from the server. Socket socket = new Socket (HOST, PORT_NUMBER ); try { dataIn = new DataInputStream (new BufferedInputStream (socket.getInputStream ())); dataOut = new DataOutputStream (new BufferedOutputStream (socket.getOutputStream ()));
for ( int i=0; i < 3; i++) { // Prompt and read an input from the user System.out.print ( "\r\nClient 1: "); System.out.flush( ); line = bufferedKbd.readLine( );
// Send the user input to the server dataOut.writeUTF ( line ); dataOut.flush ( );
// Read and display a message from another client through the Server line = dataIn.readUTF ( ); System.out.println ( "Client 2: " + line); } } catch (IOException ex) { throw ex; } finally { System.out.println ( "\r\n-- Shutdown --" ); socket.close (); } } } Thank for help!
Several possiblities if we stick to your design. In the server 1) I suggest that the for loop is traded with a "while(true)" loop. e.g. =>lblWhile: while(true){ . . . => if(endFlag) break lblWhile; =>}
Ofcourse u gotta declare a boolean called endFlag=false; at the top.
2) After the line = dataIn1.readUTF ( ); // Write a line to client 2 add =>if(line.equals("BYE"){ =>dataOut2.writeUTF ( "BYE" ); =>dataOut2.flush ( ); =>endFlag=true; =>}
do the same for dataIn2.readUTF ( );
In the client 1) Again change the for to a while loop and declare a boolean called endFlag=false; e.g. =>while(!endFlag){ . . . =>}
2) after the statement line = dataIn.readUTF ( ); add these lines. =>if(line.equals("BYE"){ =>dataOut2.writeUTF ( "BYE" ); =>dataOut2.flush ( ); =>endFlag=true; =>}