-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatServer.java
More file actions
68 lines (63 loc) · 2.58 KB
/
Copy pathChatServer.java
File metadata and controls
68 lines (63 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// ChatServer.java
import java.io.*;
import java.net.*;
import java.util.*;
public class ChatServer {
private static final int PORT = 12345; // Port number for the server
private static Set<PrintWriter> clients = new HashSet<>(); // Stores connected clients
public static void main(String[] args) {
System.out.println("Chat Server started...");
ServerSocket server = null;
try {
server = new ServerSocket(PORT); // Create server socket
while (true) {
new ClientHandler(server.accept()).start(); // Accept and handle new client connection
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (server != null) {
try {
server.close(); // Close server socket
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private static class ClientHandler extends Thread {
private Socket clientSocket; // Socket for the connected client
private PrintWriter output; // Output stream to send messages to client
public ClientHandler(Socket socket) {
this.clientSocket = socket;
}
public void run() {
try (BufferedReader input = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()))) {
output = new PrintWriter(clientSocket.getOutputStream(), true); // Send output to client
synchronized (clients) {
clients.add(output); // Add client to the list of active clients
}
String message;
while ((message = input.readLine()) != null) { // Read messages from client
System.out.println("Received: " + message);
synchronized (clients) {
for (PrintWriter writer : clients) {
writer.println(message); // Broadcast message to all clients
}
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
synchronized (clients) {
clients.remove(output); // Remove client from active clients list
}
try {
clientSocket.close(); // Close client socket
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}