-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathChatClient.java
More file actions
387 lines (317 loc) · 12 KB
/
ChatClient.java
File metadata and controls
387 lines (317 loc) · 12 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.*;
import java.util.logging.*;
/**
* Modern UDP-based chat client with Swing GUI.
*
* Compatible with modern ChatServer implementation.
*
* Features:
* - Proper resource management
* - Explicit UTF-8 encoding
* - Error message handling from server
* - Graceful shutdown
* - Better logging
* - Thread-safe GUI updates
*/
public class ChatClient extends JFrame implements AutoCloseable {
private static final Logger LOGGER = Logger.getLogger(ChatClient.class.getName());
private static final int PACKET_SIZE = 512;
private static final int SOCKET_TIMEOUT_MS = 1000;
private final DatagramSocket clientSocket;
private final InetAddress serverAddress;
private final int serverPort;
private final String nickname;
private final ExecutorService executor;
private volatile boolean running = true;
// GUI components
private final JTextArea outputArea;
private final JTextField inputField;
/**
* Creates a chat client connected to specified server.
*
* @param serverAddress Server IP address
* @param serverPort Server port
* @param nickname User's nickname
* @throws IOException if socket creation fails
*/
public ChatClient(InetAddress serverAddress, int serverPort, String nickname) throws IOException {
super("Chat Client - " + nickname);
this.serverAddress = serverAddress;
this.serverPort = serverPort;
this.nickname = nickname;
// Create UDP socket with timeout for receive operations
this.clientSocket = new DatagramSocket();
this.clientSocket.setSoTimeout(SOCKET_TIMEOUT_MS);
// Single thread for receiving messages
this.executor = Executors.newSingleThreadExecutor(r -> {
Thread t = new Thread(r, "ChatClient-Receiver-" + nickname);
t.setDaemon(false); // Want to receive remaining messages on shutdown
return t;
});
// Build GUI
this.outputArea = new JTextArea(20, 50);
this.outputArea.setEditable(false);
this.outputArea.setLineWrap(true);
this.outputArea.setWrapStyleWord(true);
this.inputField = new JTextField(40);
initializeGUI();
// Start receiving messages
executor.submit(this::receiveLoop);
// Send JOIN message to server
sendToServer("@JOIN " + nickname);
LOGGER.info("ChatClient created for " + nickname);
}
/**
* Initialize Swing GUI components.
*/
private void initializeGUI() {
// Top panel with input field and buttons
JPanel topPanel = new JPanel(new BorderLayout(5, 5));
topPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
// Input area
JPanel inputPanel = new JPanel(new BorderLayout(5, 0));
inputPanel.add(new JLabel("Message:"), BorderLayout.WEST);
inputPanel.add(inputField, BorderLayout.CENTER);
topPanel.add(inputPanel, BorderLayout.CENTER);
// Button panel
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 0));
JButton sendButton = new JButton("Send");
sendButton.addActionListener(e -> handleSendMessage());
buttonPanel.add(sendButton);
JButton quitButton = new JButton("Quit");
quitButton.addActionListener(e -> handleQuit());
buttonPanel.add(quitButton);
topPanel.add(buttonPanel, BorderLayout.EAST);
// Output area with scroll pane
JScrollPane scrollPane = new JScrollPane(outputArea);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setPreferredSize(new Dimension(600, 400));
// Layout
setLayout(new BorderLayout(5, 5));
add(topPanel, BorderLayout.NORTH);
add(scrollPane, BorderLayout.CENTER);
// Status bar
JLabel statusBar = new JLabel("Connected to " + serverAddress.getHostAddress() + ":" + serverPort);
statusBar.setBorder(BorderFactory.createEmptyBorder(2, 5, 2, 5));
add(statusBar, BorderLayout.SOUTH);
// Input field action (Enter key sends message)
inputField.addActionListener(e -> handleSendMessage());
// Window closing handler
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
handleQuit();
}
});
// Finalize window
pack();
setLocationRelativeTo(null); // Center on screen
setVisible(true);
// Focus on input field
inputField.requestFocusInWindow();
}
/**
* Handle send message action.
*/
private void handleSendMessage() {
String message = inputField.getText().trim();
if (message.isEmpty()) {
return;
}
// Don't allow messages that look like commands (user confusion)
if (message.startsWith("@")) {
appendToOutput("ERROR: Messages cannot start with '@' (reserved for commands)");
return;
}
// Send to server (server will add nickname prefix)
sendToServer(message);
// Clear input field
inputField.setText("");
inputField.requestFocusInWindow();
}
/**
* Handle quit action.
*/
private void handleQuit() {
int choice = JOptionPane.showConfirmDialog(
this,
"Are you sure you want to quit?",
"Confirm Quit",
JOptionPane.YES_NO_OPTION
);
if (choice == JOptionPane.YES_OPTION) {
sendToServer("@QUIT");
// Give server time to process QUIT
try {
Thread.sleep(100);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
close();
}
}
/**
* Send message to server.
*/
private void sendToServer(String message) {
try {
byte[] data = message.getBytes(StandardCharsets.UTF_8);
// Check message size
if (data.length > PACKET_SIZE) {
appendToOutput("ERROR: Message too long (max " + PACKET_SIZE + " bytes)");
LOGGER.warning("Message too long: " + data.length + " bytes");
return;
}
DatagramPacket packet = new DatagramPacket(
data, data.length,
serverAddress, serverPort
);
clientSocket.send(packet);
LOGGER.fine("Sent to server: " + message);
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Failed to send message", e);
appendToOutput("ERROR: Failed to send message - " + e.getMessage());
}
}
/**
* Receive loop - runs in background thread.
*/
private void receiveLoop() {
byte[] buffer = new byte[PACKET_SIZE];
LOGGER.info("Receive loop started");
while (running) {
try {
// Create fresh packet for each receive
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
// Block waiting for packet (with timeout)
clientSocket.receive(packet);
// Extract message with explicit encoding
byte[] data = java.util.Arrays.copyOfRange(
packet.getData(), 0, packet.getLength()
);
String message = new String(data, StandardCharsets.UTF_8);
// Display message in GUI (thread-safe)
appendToOutput(message);
LOGGER.fine("Received: " + message);
// Check for special server messages
if (message.startsWith("OK:") || message.startsWith("ERROR:")) {
// Server response to our command
if (message.contains("Goodbye")) {
LOGGER.info("Server confirmed quit");
}
}
} catch (SocketTimeoutException e) {
// No message received within timeout - this is normal, continue
} catch (SocketException e) {
// Socket closed (during shutdown) - expected
if (running) {
LOGGER.log(Level.SEVERE, "Socket error", e);
}
break;
} catch (IOException e) {
LOGGER.log(Level.WARNING, "Error receiving message", e);
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Unexpected error in receive loop", e);
}
}
LOGGER.info("Receive loop terminated");
}
/**
* Append message to output area (thread-safe).
* Must be called from any thread.
*/
private void appendToOutput(String message) {
// Use SwingUtilities to ensure thread-safe GUI updates
SwingUtilities.invokeLater(() -> {
outputArea.append(message + "\n");
// Auto-scroll to bottom
outputArea.setCaretPosition(outputArea.getDocument().getLength());
});
}
/**
* Gracefully close the client.
*/
@Override
public void close() {
if (!running) {
return; // Already closing
}
LOGGER.info("Closing ChatClient for " + nickname);
running = false;
// Close socket (interrupts receive())
clientSocket.close();
// Shutdown executor
executor.shutdown();
try {
if (!executor.awaitTermination(2, TimeUnit.SECONDS)) {
executor.shutdownNow();
}
} catch (InterruptedException e) {
executor.shutdownNow();
Thread.currentThread().interrupt();
}
// Close window
dispose();
LOGGER.info("ChatClient closed");
}
/**
* Launch a chat client.
*
* @param serverAddress Server IP address
* @param serverPort Server port
* @param nickname User nickname
* @return ChatClient instance
* @throws IOException if connection fails
*/
public static ChatClient launch(InetAddress serverAddress, int serverPort, String nickname)
throws IOException {
return new ChatClient(serverAddress, serverPort, nickname);
}
/**
* Launch a chat client connecting to localhost.
*/
public static ChatClient launch(int serverPort, String nickname) throws IOException {
return launch(InetAddress.getLoopbackAddress(), serverPort, nickname);
}
public static void main(String[] args) {
if (args.length != 2) {
System.err.println("USAGE: java ChatClient <port> <nickname>");
System.exit(1);
}
try {
int port = Integer.parseInt(args[0]);
String nickname = args[1];
// Validate nickname
if (nickname.isEmpty() || nickname.length() > 20) {
System.err.println("ERROR: Nickname must be 1-20 characters");
System.exit(1);
}
if (!nickname.matches("[a-zA-Z0-9_-]+")) {
System.err.println("ERROR: Nickname must be alphanumeric (plus _ and -)");
System.exit(1);
}
// Launch client
ChatClient client = launch(port, nickname);
LOGGER.info("ChatClient launched successfully");
} catch (NumberFormatException e) {
System.err.println("ERROR: Invalid port number");
System.exit(1);
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Failed to start client", e);
JOptionPane.showMessageDialog(
null,
"Failed to connect to server:\n" + e.getMessage(),
"Connection Error",
JOptionPane.ERROR_MESSAGE
);
System.exit(1);
}
}
}