-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatServer3.java
More file actions
120 lines (105 loc) · 4.34 KB
/
ChatServer3.java
File metadata and controls
120 lines (105 loc) · 4.34 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import java.io.*;
import java.net.*;
import java.util.*;
public class ChatServer3 {
private static final int PORT = 5000;
private static List<ClientHandler> clients = Collections.synchronizedList(new ArrayList<>());
private static volatile boolean serverRunning = true;
public static void main(String[] args) {
try (ServerSocket serverSocket = new ServerSocket(PORT);
Scanner scanner = new Scanner(System.in)) {
System.out.println("Server started on port " + PORT);
// Thread for handling server console input (closing server)
Thread serverInputThread = new Thread(() -> {
while (serverRunning) {
String input = scanner.nextLine();
if (input.equalsIgnoreCase("q")) {
serverRunning = false;
closeAllClients();
break;
}
}
});
serverInputThread.start();
// Server main loop: Accept new clients
while (serverRunning) {
Socket clientSocket = serverSocket.accept();
System.out.println("New client connected: " + clientSocket);
ClientHandler clientHandler = new ClientHandler(clientSocket);
clients.add(clientHandler);
new Thread(clientHandler).start();
}
} catch (IOException e) {
if (serverRunning) e.printStackTrace();
}
}
// Close all client connections and exit
private static synchronized void closeAllClients() {
for (ClientHandler client : clients) {
client.closeConnection();
}
System.out.println("Server shutting down...");
System.exit(0);
}
static class ClientHandler implements Runnable {
private Socket socket;
private BufferedReader in;
private PrintWriter out;
private volatile boolean flag = true;
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);
Scanner serverInput = new Scanner(System.in);
// Write Thread: Sends messages from server to client
Thread writeThread = new Thread(() -> {
while (flag) {
String serverMessage = serverInput.nextLine();
out.println(serverMessage);
if (serverMessage.equalsIgnoreCase("bye")) {
flag = false;
}
}
});
// Read Thread: Reads messages from the client
Thread readThread = new Thread(() -> {
try {
while (flag) {
String clientMessage = in.readLine();
if (clientMessage == null || clientMessage.equalsIgnoreCase("bye")) {
flag = false;
break;
}
System.out.println("Client: " + clientMessage);
}
} catch (IOException e) {
System.out.println("Client disconnected.");
}
});
writeThread.start();
readThread.start();
writeThread.join();
readThread.join();
} catch (IOException | InterruptedException e) {
System.out.println("Error handling client.");
} finally {
closeConnection();
}
}
public void closeConnection() {
try {
flag = false;
if (in != null) in.close();
if (out != null) out.close();
if (socket != null) socket.close();
System.out.println("Client disconnected.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}