|
@ -20,12 +20,16 @@ public class ClientHandler implements Runnable { |
|
|
public void run() { |
|
|
public void run() { |
|
|
|
|
|
|
|
|
try { |
|
|
try { |
|
|
|
|
|
// Set up input and output streams for communication with the client |
|
|
fromClientReader = new BufferedReader (new InputStreamReader(connectionToClient.getInputStream())); |
|
|
fromClientReader = new BufferedReader (new InputStreamReader(connectionToClient.getInputStream())); |
|
|
toClientWriter = new PrintWriter (new OutputStreamWriter(connectionToClient.getOutputStream())); |
|
|
toClientWriter = new PrintWriter (new OutputStreamWriter(connectionToClient.getOutputStream())); |
|
|
chatServer.broadcastMessage(name + " connected."); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
chatServer.broadcastMessage(name + " connected."); // Broadcast a message when the client is connected |
|
|
|
|
|
|
|
|
|
|
|
// Read messages from the client and broadcast them to all clients |
|
|
String message = fromClientReader.readLine(); |
|
|
String message = fromClientReader.readLine(); |
|
|
while (message!=null) { |
|
|
while (message!=null) { |
|
|
|
|
|
// Broadcast the client's message to all connected clients |
|
|
chatServer.broadcastMessage(name + ": " + message); |
|
|
chatServer.broadcastMessage(name + ": " + message); |
|
|
message = fromClientReader.readLine(); |
|
|
message = fromClientReader.readLine(); |
|
|
} |
|
|
} |
|
|