1 Commits
3 changed files with 65 additions and 20 deletions
+32 -8
View File
@@ -37,19 +37,43 @@ public class ChatClient {
} }
private void connectToServer() throws IOException { private void connectToServer() throws IOException {
// TODO: create socket connection to host:port // ساخت اتصال سوکت به سرور با هاست و پورت داده شده
// TODO: initialize output stream (PrintWriter) this.socket = new Socket(host, port);
// TODO: initialize input stream (BufferedReader) // راه‌اندازی استریم خروجی برای ارسال پیام به سرور (با قابلیت autoflush)
this.out = new PrintWriter(socket.getOutputStream(), true);
// راه‌اندازی استریم ورودی برای دریافت پیام‌ها از سرور
this.in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} }
private void readUserInputAndSend() throws IOException { private void readUserInputAndSend() throws IOException {
// TODO: read continuously from console String line;
// TODO: send each input line to server // خواندن مداوم خطوط تایپ شده توسط کاربر در ترمینال تا زمانی که کاربر متوقفش کنه
while ((line = consoleReader.readLine()) != null) {
out.println(line);
}
} }
private void cleanup() { private void cleanup() {
// TODO: close socket safely // بستن ایمن تمام هندل‌ها و سوکت سیستم‌عامل برای آزاد کردن پورت
// TODO: release resources try {
if (in != null) in.close();
} catch (IOException ignored) {}
try {
if (out != null) out.close();
} catch (Exception ignored) {} // اینجا نوع استثنا اضافه شد
try {
if (consoleReader != null) consoleReader.close();
} catch (IOException ignored) {}
try {
if (socket != null && !socket.isClosed()) {
socket.close();
}
} catch (IOException e) {
System.err.println("Error closing client socket: " + e.getMessage());
}
} }
public static void main(String[] args) { public static void main(String[] args) {
@@ -69,4 +93,4 @@ public class ChatClient {
new ChatClient(host, port).start(); new ChatClient(host, port).start();
} }
} }
+21 -9
View File
@@ -39,20 +39,32 @@ public class ChatServer {
} }
public void broadcast(String message, ClientHandler sender) { public void broadcast(String message, ClientHandler sender) {
// step 1: iterate through connectedClients safely // قفل کردن متقابل روی کلاینت‌ها برای جلوگیری از تداخل موازی تردها (ConcurrentModificationException)
// step 2: skip the sender client synchronized (connectedClients) {
// step 3: send message to remaining clients using sendMessage for (ClientHandler client : connectedClients) {
// پیام رو به همه میفرستیم بجز خود فرستنده اصلی
if (client != sender) {
client.sendMessage(message);
}
}
}
} }
public void broadcastSystemMessage(String message) { public void broadcastSystemMessage(String message) {
// step 1: iterate through all connected clients // پیمایش ایمن لیست برای فرستادن پیام‌های سیستمی سرور
// step 2: send system message to each client synchronized (connectedClients) {
for (ClientHandler client : connectedClients) {
client.sendMessage(message);
}
}
} }
public void removeClient(ClientHandler clientHandler) { public void removeClient(ClientHandler clientHandler) {
// step 1: remove client from connectedClients set // حذف کلاینت از ست سراسری کاربران آنلاین
// step 2: retrieve username from clientHandler connectedClients.remove(clientHandler);
// step 3: broadcast system message about user leaving // گرفتن نام کاربری برای اعلام خروج به بقیه
String username = clientHandler.getUsername();
broadcastSystemMessage("[System] " + username + " left the chat.");
} }
public static void main(String[] args) { public static void main(String[] args) {
@@ -66,4 +78,4 @@ public class ChatServer {
} }
new ChatServer(port).start(); new ChatServer(port).start();
} }
} }
+12 -3
View File
@@ -13,7 +13,16 @@ public class MessageListener implements Runnable {
@Override @Override
public void run() { public void run() {
// TODO: continuously read messages from server try {
// TODO: print received messages immediately String serverMessage;
// خواندن مداوم پیام‌های ارسالی از سرور
while ((serverMessage = inputReader.readLine()) != null) {
// چاپ سریع پیام دریافت شده در ترمینال کاربر
System.out.println(serverMessage);
}
} catch (IOException e) {
// وقتی اتصال به هر دلیلی بسته یا خراب بشه وارد این بخش میشیم
System.out.println("[System] Connection to server lost.");
}
} }
} }