Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8ad8f05df8 |
@@ -37,19 +37,43 @@ public class ChatClient {
|
||||
}
|
||||
|
||||
private void connectToServer() throws IOException {
|
||||
// TODO: create socket connection to host:port
|
||||
// TODO: initialize output stream (PrintWriter)
|
||||
// TODO: initialize input stream (BufferedReader)
|
||||
// ساخت اتصال سوکت به سرور با هاست و پورت داده شده
|
||||
this.socket = new Socket(host, port);
|
||||
// راهاندازی استریم خروجی برای ارسال پیام به سرور (با قابلیت autoflush)
|
||||
this.out = new PrintWriter(socket.getOutputStream(), true);
|
||||
// راهاندازی استریم ورودی برای دریافت پیامها از سرور
|
||||
this.in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
|
||||
}
|
||||
|
||||
private void readUserInputAndSend() throws IOException {
|
||||
// TODO: read continuously from console
|
||||
// TODO: send each input line to server
|
||||
String line;
|
||||
// خواندن مداوم خطوط تایپ شده توسط کاربر در ترمینال تا زمانی که کاربر متوقفش کنه
|
||||
while ((line = consoleReader.readLine()) != null) {
|
||||
out.println(line);
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
|
||||
@@ -39,20 +39,32 @@ public class ChatServer {
|
||||
}
|
||||
|
||||
public void broadcast(String message, ClientHandler sender) {
|
||||
// step 1: iterate through connectedClients safely
|
||||
// step 2: skip the sender client
|
||||
// step 3: send message to remaining clients using sendMessage
|
||||
// قفل کردن متقابل روی کلاینتها برای جلوگیری از تداخل موازی تردها (ConcurrentModificationException)
|
||||
synchronized (connectedClients) {
|
||||
for (ClientHandler client : connectedClients) {
|
||||
// پیام رو به همه میفرستیم بجز خود فرستنده اصلی
|
||||
if (client != sender) {
|
||||
client.sendMessage(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) {
|
||||
// step 1: remove client from connectedClients set
|
||||
// step 2: retrieve username from clientHandler
|
||||
// step 3: broadcast system message about user leaving
|
||||
// حذف کلاینت از ست سراسری کاربران آنلاین
|
||||
connectedClients.remove(clientHandler);
|
||||
// گرفتن نام کاربری برای اعلام خروج به بقیه
|
||||
String username = clientHandler.getUsername();
|
||||
broadcastSystemMessage("[System] " + username + " left the chat.");
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
@@ -13,7 +13,16 @@ public class MessageListener implements Runnable {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
// TODO: continuously read messages from server
|
||||
// TODO: print received messages immediately
|
||||
try {
|
||||
String serverMessage;
|
||||
// خواندن مداوم پیامهای ارسالی از سرور
|
||||
while ((serverMessage = inputReader.readLine()) != null) {
|
||||
// چاپ سریع پیام دریافت شده در ترمینال کاربر
|
||||
System.out.println(serverMessage);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
// وقتی اتصال به هر دلیلی بسته یا خراب بشه وارد این بخش میشیم
|
||||
System.out.println("[System] Connection to server lost.");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user