complete client

This commit is contained in:
2026-06-27 18:14:16 +03:30
parent 240eac0504
commit faac68e30b
4 changed files with 156 additions and 32 deletions
@@ -1,18 +1,40 @@
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){
// (filename + sender), it's already if(((ChatMessage) objectInput).getType() == MessageType.LOGIN_SUCCESS){
// saved to disk by the server. System.out.println("Login success");
} else if (((ChatMessage) objectInput).getType() == MessageType.LOGIN_FAILED) {
System.out.println("Login Failed");
}
System.out.println(((ChatMessage) objectInput).getSender()+ ": " +
((ChatMessage) objectInput).getContent());
}
else if(objectInput instanceof FileMessage){
System.out.println("File name: " + ((FileMessage) objectInput).getFilename()
+ "from : "+ ((FileMessage) objectInput).getSender()+ " recived.");
}
}
} catch (Exception e){ } catch (Exception e){
System.out.println("Disconnected from server"); System.out.println("Disconnected from server");
} }
@@ -1,26 +1,76 @@
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();
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().trim();
// - "/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] , parts[2]));
break;
}
case "/users":
{
out.writeObject(new ChatMessage(MessageType.USER_LIST ,username
, "Server" , null));
break;
}
case "/sendfile":
{
byte[] dataofFile = Files.readAllBytes(Path.of(parts[2]));
out.writeObject(new FileMessage(username , parts[1] , parts[2] , dataofFile ));
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,28 @@
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 private static final UserManager users = new UserManager();
// broadcasting and private messaging work correctly.
public static void main(String[] args) { public static void main(String[] args) {
// TODO: Create a ServerSocket
// TODO: In an infinite loop: try {
// accept an incoming client connection ServerSocket srvSocket = new ServerSocket(5000);
// make a new thread running ClientSession for each user.
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());
}
} }
} }
@@ -2,16 +2,37 @@ 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.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;
public class ClientSession implements Runnable { public class ClientSession implements Runnable {
private String username; private String username;
private ObjectOutputStream out;
private ObjectInputStream in;
private Socket socket;
private UserManager userManage;
public ClientSession(Socket socket, UserManager userManager) { public ClientSession(Socket socket, UserManager userManager) {
this.socket = socket;
this.userManage = userManager;
try {
out = new ObjectOutputStream(socket.getOutputStream());
out.flush();
in = new ObjectInputStream(socket.getInputStream());
} catch (IOException e) {
throw new RuntimeException(e);
}
// TODO : Create an ObjectOutputStream from socket.getOutputStream() // TODO : Create an ObjectOutputStream from socket.getOutputStream()
// and an ObjectInputStream from socket.getInputStream(). // and an ObjectInputStream from socket.getInputStream().
} }
@@ -20,6 +41,24 @@ public class ClientSession implements Runnable {
public void run() { public void run() {
try { try {
ChatMessage loginmsg = (ChatMessage) in.readObject();
String username = loginmsg.getSender();
if(loginmsg.getType() != MessageType.LOGIN){
out.writeObject(new ChatMessage(MessageType.LOGIN_FAILED , "server"
, username,"login failed"));
socket.close();
return;
}
out.writeObject(new ChatMessage(MessageType.LOGIN_SUCCESS , "server" ,
null , "welcome to messanger"));
userManage.addUser(username , this);
// TODO: Welcome the user (login step) // TODO: Welcome the user (login step)
// 1. Read the first object sent by the client. // 1. Read the first object sent by the client.
// 2. Check it's a ChatMessage with type LOGIN. // 2. Check it's a ChatMessage with type LOGIN.