0% found this document useful (0 votes)
17 views3 pages

JPractical 16

Uploaded by

vedived92
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views3 pages

JPractical 16

Uploaded by

vedived92
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

import java.io.

*;
import java.net.*;
import java.util.*;

public class Practical16 {


private static final int PORT = 1234; // Server port number
private static Set<ClientHandler> clientHandlers = new
HashSet<>(); // To store connected clients

public static void main(String[] args) {


try (ServerSocket serverSocket = new ServerSocket(PORT)) {
System.out.println("Chat server started on port " +
PORT);

while (true) {
// Accept incoming client connections
Socket socket = serverSocket.accept();
System.out.println("New client connected: " +
socket.getInetAddress());

// Create a new ClientHandler for each client and


start it in a new thread
ClientHandler clientHandler = new
ClientHandler(socket);
clientHandlers.add(clientHandler);
new Thread(clientHandler).start();
}
} catch (IOException e) {
System.err.println("Error starting server: " +
e.getMessage());
}
}
// Method to broadcast messages to all connected clients
static void broadcastMessage(String message, ClientHandler
sender) {
for (ClientHandler clientHandler : clientHandlers) {
if (clientHandler != sender) {
clientHandler.sendMessage(message);
}
}
}
// Method to remove a client when it disconnects
static void removeClient(ClientHandler clientHandler) {
clientHandlers.remove(clientHandler);
}
}
// ClientHandler class to manage individual client connections
class ClientHandler implements Runnable {
private Socket socket;
private PrintWriter out;
private BufferedReader in;
public ClientHandler(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
String message;
// Read messages from the client
while ((message = in.readLine()) != null) {
System.out.println("Received: " + message);
// Broadcast the message to all other clients
Practical16.broadcastMessage(message, this);
}
} catch (IOException e) {
System.err.println("Client disconnected: " +
e.getMessage());
} finally {
closeConnection();
}
}
// Send a message to the client
void sendMessage(String message) {
out.println(message);
}
// Close client connection
private void closeConnection() {
try {
if (in != null) in.close();
if (out != null) out.close();
if (socket != null) socket.close();
Practical16.removeClient(this);
} catch (IOException e) {
System.err.println("Error closing connection: " +
e.getMessage());
}
}
}

You might also like