Java Client-Server Chat Issues and Ideas

 
 
 
 

Specifications of a "Client-Server" based Java Chat system.

 

  • You will create a Java Chat Server application that uses ServerSocket class to listen on a particular TCP port (e.g. 50001). Your server will wait for a connection from a Java GUI Chat Client on this port and spin off from this a Socket and connection. Note more that one Java GUI Chat Client can try to connect and a Socket and stream for each should be created (see class lecture tips and discussion).
  • Java GUI Client Application can run on any machine with a JVM. This GUI Chat Client Application will use Sockets to communicate with the Java Chat Server Application over the same chosen port.
  • The Java GUI Chat Client will send either "Connection" or "Data" messages to the Java Chat Server Application over the socket. "Connection" message will be either about connecting or disconnecting to chat. "Data" message is simply a chat message the client is trying to send out to the chat room.
  • The Java GUI Chat Client Application upon startup opens a Socket and connection to the port number the Java Chat Server is listening to. It then sends a message saying it wants to "connect" to the chat room, sending along with this identification information, including a "user handle" that is used as the person's identification in the chat room. These user handles of the people actively connected in chat are displayed in each Java GUI Chat Client Application being run.
  • Once you have entered into chat, the user can see the current text messages as well as there is a location for them to type in a new message and send it.
  • From a Java GUI Chat Client, you should have the ability to leave a chat session. This will cause the Java GUI Client to send another "Connection" message this time requesting a disconnect. As a result, any streams and thread associated with this client in the Java Chat Server should be closed. Also, all of the other active Java GUI Chat Clients should recieve a message that updates their list of user handles currently in the chat room so that this person's handle no longer appears.
  • From a Java GUI Chat Client you should have the ability to save the current displayed session text messages to a file.

Java Chat Server Code

I am not going to specify the entire code, but, I am going to get you started thinking in the right direction. See comments we discuss in class lecture about this.

ServerSocket ss;

Socket clientSocket;
BufferedReader in;

 

ServerSocket ss = new ServerSocket(host, 50001);

 

......//somewhere later in code

while(true) {

//listen until connection attemp on port
//spin off socket to client trying to
// connect
clientSocket = ss.accept();

//You should save this Socket somewhere
//and from it spin off an InputStream
//using getInputStream() method to read

//incomming messages>>>USE Classes
//to organize or collect all of this info.


//Here I am just going to spin around in
//a loop waiting for input
in = new BufferedReader( new InputStreamReader(clientSocket.getInputStream()));

while((String i = in.readLine()) != null)
        System.out.println(i);

clientSocket.close();


}

 

What is WRONG with the above code??????

ANSWER: you can only handle ONE client connecting at a time.

SOLUTION: Use threads, one for each client the server is communicating with. See here for an unrelated example!!!

 

 

Java GUI Chat Client Code

I am not going to specify the entire code, but, I am going to get you started thinking in the right direction about the communication logic. See comments we discuss in class lecture about this.

Socket socket;
String host;


BufferedReader in;
PrintStream out;

......//later on in code

//Client it to try to connect to host over predetermined port
//you should save the socket in a CLASS (OOP design!)
socket = new Socket(host, 50001);




//Open up stream to write out messages over socket
//you should again save in a CLASS
out = new PrintStream(socket.getOutputStream());

//Open up stream to recieve any messages over socket from
//server. Again should save in a CLASS
in = new BufferedReader(new InputStreamReader(socket.getInputStream()) );

 

.....//later in code

//Now you can read using in or write using out to the chat
//server using information from your GUI the user is using.

//example....send the text stored in the string S

out.println(S);

//example....reading in information
in.readLine();

 

 

}

 

What is WRONG with the above code??????

PROBLEM: How do you know when to check if the server is sending you any information....do you need an infinite loop???

SOLUTION1: Maybe you could have 2 threads in your client. One to continually check for new input from the server to then process as needed (e.g. incomming chat messages from server sent by other users). The 2nd thread would be used to process your GUI and respond in some casses by sending output messages to your server.