2 Commits
Author SHA1 Message Date
mahdi_Goudarzi 027ead9fa9 debug and test 2026-06-28 10:02:55 +03:30
mahdi_Goudarzi faac68e30b complete client 2026-06-27 18:14:16 +03:30
4 changed files with 228 additions and 54 deletions
@@ -1,20 +1,42 @@
package com.university.chat.Client; package com.university.chat.Client;
import com.university.chat.Common.ChatMessage;
import com.university.chat.Common.FileMessage;
import com.university.chat.Common.MessageType;
import java.io.ObjectInputStream;
public class ServerListener implements Runnable{ public class ServerListener implements Runnable{
// TODO: store the ObjectInputStream from the user socket
// (this should be the same input stream the private ObjectInputStream in ;
// chatClient created when connecting)
public ServerListener(ObjectInputStream ois){
this.in = ois;
}
@Override @Override
public void run() { public void run() {
try { try {
// TODO: In an infinite loop read objects from the server while (true){
// - if it's a ChatMessage -> print "<sender>: <content>" Object objectInput = in.readObject();
// - if it's a FileMessage -> print that a file was received if(objectInput instanceof ChatMessage msg){
// (filename + sender), it's already if(msg.getType() == MessageType.LOGIN_SUCCESS){
// saved to disk by the server. System.out.println("Login success");
} else if (msg.getType() == MessageType.LOGIN_FAILED) {
System.out.println("Login Failed");
}
System.out.println(msg.getSender()+ ": " + msg.getContent());
}
else if(objectInput instanceof FileMessage filemsg){
System.out.println("File name: "+ filemsg.getFilename()
+"from : "+ filemsg.getSender()+ " recived.");
}
}
} catch (Exception e){ } catch (Exception e){
System.out.println("Disconnected from server"); System.out.println("Disconnected from server");
System.out.println(e.getMessage());
} }
} }
} }
@@ -1,26 +1,81 @@
package com.university.chat.Client; package com.university.chat.Client;
import com.university.chat.Common.ChatMessage;
import com.university.chat.Common.FileMessage;
import com.university.chat.Common.MessageType;
import java.io.*;
import java.net.Socket;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Scanner;
public class chatClient { public class chatClient {
public static void main() {
// TODO: Connecting to the server
// 1. Create a socket and connect to the server public static void main() throws IOException {
// 2. Create an ObjectOutputStream (out) and ObjectInputStream (in) Scanner input = new Scanner(System.in);
// from the socket's streams — output FIRST, then input. Socket socket = new Socket("127.0.0.1" , 5000);
// 2. Get the username, and send a LOGIN ChatMessage with that username ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
// 3. Start a new Thread running a ServerListener(in) so incoming ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
// messages are handled concurrently.
System.out.println("Connected to Server . Please enter user name:");
String username = input.nextLine();
out.writeObject(new ChatMessage(MessageType.LOGIN , username , "Server" , "Login message"));
out.flush();
ServerListener serverListener = new ServerListener(in);
Thread t = new Thread(serverListener);
t.start();
while (true){ while (true){
try { try {
// TODO: Program loop — read a line from the console and act on it: String promt = input.nextLine();
// - "/msg <user> <text>" -> build & send a PRIVATE_MESSAGE //promt = promt.replace("<", "").replace(">", "");
// - "/users" -> build & send a USER_LIST request
// - "/sendfile <user> <path>" -> read the file into a byte[] String[] parts = promt.split(" " , 3);
// (you can use TransferProgress
// to show progress) switch (parts[0]){
// and send it as a FileMessage case "/msg":
// - anything else -> send a PUBLIC_MESSAGE {
// Remember to flush() the output stream after writeObject(). out.writeObject(new ChatMessage(MessageType.PRIVATE_MESSAGE , username
, parts[1] ," \'private\' " + parts[2]));
break;
}
case "/users":
{
out.writeObject(new ChatMessage(MessageType.USER_LIST ,username
, "Server" , null));
out.flush();
break;
}
case "/sendfile":
{
File file = new File(String.valueOf(Path.of(parts[2])));
String filename = file.getName();
byte[] fileContent = Files.readAllBytes(file.toPath());
out.writeObject(new FileMessage(username , parts[1] , parts[2] , fileContent ));
out.flush();
System.out.println("file send.");
break;
}
case "/quit":
{
System.out.println("Goodby!");
return;
}
default:
{
out.writeObject(new ChatMessage(MessageType.PUBLIC_MESSAGE , username ,
"All" , promt));
break;
}
}
out.flush();
} catch (Exception e){ } catch (Exception e){
System.out.println("command failed: " + e.getMessage()); System.out.println("command failed: " + e.getMessage());
} }
@@ -1,15 +1,32 @@
package com.university.chat.Server; package com.university.chat.Server;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class ChatServer { public class ChatServer {
// TODO: declare a single shared UserManager instance (static final)
// This MUST be shared by all ClientSession threads so that
// broadcasting and private messaging work correctly.
public static void main(String[] args) { private static final UserManager users = new UserManager();
// TODO: Create a ServerSocket private static ServerSocket srvSocket;
// TODO: In an infinite loop: public static void main(String[] args) throws IOException {
// accept an incoming client connection
// make a new thread running ClientSession for each user. try {
srvSocket = new ServerSocket(5000);
System.out.println("Servre start.");
while (true){
Socket clientsocket = srvSocket.accept();
ClientSession clientSession = new ClientSession(clientsocket , users);
Thread t = new Thread(clientSession);
t.start();
}
} catch (IOException e) {
System.out.println(e.getMessage());
}finally {
srvSocket.close();
}
} }
} }
@@ -2,44 +2,89 @@ package com.university.chat.Server;
import com.university.chat.Common.ChatMessage; import com.university.chat.Common.ChatMessage;
import com.university.chat.Common.FileMessage; import com.university.chat.Common.FileMessage;
import com.university.chat.Common.MessageType;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket; import java.net.Socket;
import java.nio.file.Files; import java.nio.file.Files;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class ClientSession implements Runnable { public class ClientSession implements Runnable {
private String username; private String username;
private ObjectOutputStream out = null;
private ObjectInputStream in = null;
private final Socket socket;
private final UserManager userManage;
private final Lock L = new ReentrantLock();
public ClientSession(Socket socket, UserManager userManager) { public ClientSession(Socket socket, UserManager userManager) {
// TODO : Create an ObjectOutputStream from socket.getOutputStream() this.socket = socket;
// and an ObjectInputStream from socket.getInputStream(). this.userManage = userManager;
try {
out = new ObjectOutputStream(socket.getOutputStream());
in = new ObjectInputStream(socket.getInputStream());
} catch (IOException e) {
System.out.println(e.getMessage());
}
} }
@Override @Override
public void run() { public void run() {
try { try {
// TODO: Welcome the user (login step) ChatMessage loginmsg = (ChatMessage) in.readObject();
// 1. Read the first object sent by the client. username = loginmsg.getSender();
// 2. Check it's a ChatMessage with type LOGIN.
// 3. Extract the username.
// 4. Try to register the user via userManager.addUser(...). if(loginmsg.getType() != MessageType.LOGIN){
// 5. If the username is taken, send back LOGIN_FAILED and close the socket. out.writeObject(new ChatMessage(MessageType.LOGIN_FAILED , "server"
// 6. Otherwise, create the user's folders with FileManager.createUserFolders(...) , username,"login failed"));
// and send back LOGIN_SUCCESS. socket.close();
return;
}
FileManager.createUserFolders(username);
out.writeObject(new ChatMessage(MessageType.LOGIN_SUCCESS , "server" ,
username , "welcome to messanger login succes"));
System.out.println(username + " connected");
userManage.addUser(username , this);
while (true){
Object inputobj = in.readObject();
if(inputobj == null){
continue;
}
if(inputobj instanceof ChatMessage msg){
handleChatMessage(msg);
} else if (inputobj instanceof FileMessage filemsg) {
handleFileMessage(filemsg);
}
}
// TODO: Main message loop
// In a loop, call in.readObject(), you can separate messages by their type:
// - if it's a ChatMessage -> call handleChatMessage(msg)
// - if it's a FileMessage -> call handleFileMessage(fileMsg)
// Keep looping until the connection is closed (an exception will be thrown).
} catch (Exception e) { } catch (Exception e) {
System.out.println("Disconnected: " + username); System.out.println("Disconnected: " + username);
System.out.println(e.getMessage());
} finally { } finally {
// TODO: Remove the user from UserManager so they no longer if(username != null) {
// receive broadcasts or appear in users list userManage.removeUser(username);
}
try {
socket.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
} }
} }
@@ -47,13 +92,26 @@ public class ClientSession implements Runnable {
private void handleChatMessage(ChatMessage msg) throws IOException { private void handleChatMessage(ChatMessage msg) throws IOException {
switch (msg.getType()) { switch (msg.getType()) {
case PUBLIC_MESSAGE -> { case PUBLIC_MESSAGE -> {
// TODO: Broadcast this message to every connected client. for(ClientSession cs : userManage.getAllSessions()){
if(cs != this){
cs.sendMessage(msg);
}
}
} }
case PRIVATE_MESSAGE -> { case PRIVATE_MESSAGE -> {
// TODO: Forward this message to the receiver user. ClientSession cs = userManage.getUser(msg.getReceiver());
if (cs != null) {
cs.sendMessage(msg);
}
} }
case USER_LIST -> { case USER_LIST -> {
// TODO: Reply to the requester with the list of online users. ClientSession cs = userManage.getUser(msg.getSender());
ChatMessage listReply = new ChatMessage(MessageType.USER_LIST, "server",
username, userManage.listUsers());
if (cs != null) {
cs.sendMessage(listReply);
}
} }
} }
} }
@@ -66,6 +124,28 @@ public class ClientSession implements Runnable {
Files.write(sentPath, fileMsg.getData()); Files.write(sentPath, fileMsg.getData());
Files.write(recvPath, fileMsg.getData()); Files.write(recvPath, fileMsg.getData());
// TODO: Forward the received file-message to the destination user. ClientSession cs = userManage.getUser(fileMsg.getReceiver());
if(cs != null){
cs.sendMessage(fileMsg);
}
System.out.println("Server received file: " + fileMsg.getFilename() + " with size: " + fileMsg.getData().length);
}
public void sendMessage(Object object) {
try {
L.lock();
out.writeObject(object);
out.flush();
}
catch (IOException e) {
System.out.println(e.getMessage());
}
finally {
L.unlock();
} }
} }
}