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;
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{
// TODO: store the ObjectInputStream from the user socket
// (this should be the same input stream the
// chatClient created when connecting)
private ObjectInputStream in ;
public ServerListener(ObjectInputStream ois){
this.in = ois;
}
@Override
public void run() {
try {
// TODO: In an infinite loop read objects from the server
// - if it's a ChatMessage -> print "<sender>: <content>"
// - if it's a FileMessage -> print that a file was received
// (filename + sender), it's already
// saved to disk by the server.
while (true){
Object objectInput = in.readObject();
if(objectInput instanceof ChatMessage){
if(((ChatMessage) objectInput).getType() == MessageType.LOGIN_SUCCESS){
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){
System.out.println("Disconnected from server");
}
@@ -1,26 +1,76 @@
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 static void main() {
// TODO: Connecting to the server
// 1. Create a socket and connect to the server
// 2. Create an ObjectOutputStream (out) and ObjectInputStream (in)
// from the socket's streams — output FIRST, then input.
// 2. Get the username, and send a LOGIN ChatMessage with that username
// 3. Start a new Thread running a ServerListener(in) so incoming
// messages are handled concurrently.
public static void main() throws IOException {
Scanner input = new Scanner(System.in);
Socket socket = new Socket("127.0.0.1" , 5000);
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
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){
try {
// TODO: Program loop — read a line from the console and act on it:
// - "/msg <user> <text>" -> build & send a PRIVATE_MESSAGE
// - "/users" -> build & send a USER_LIST request
// - "/sendfile <user> <path>" -> read the file into a byte[]
// (you can use TransferProgress
// to show progress)
// and send it as a FileMessage
// - anything else -> send a PUBLIC_MESSAGE
// Remember to flush() the output stream after writeObject().
String promt = input.nextLine().trim();
promt = promt.replace("<", "").replace(">", "");
String[] parts = promt.split(" " , 3);
switch (parts[0]){
case "/msg":
{
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){
System.out.println("command failed: " + e.getMessage());
}
@@ -1,15 +1,28 @@
package com.university.chat.Server;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
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.
private static final UserManager users = new UserManager();
public static void main(String[] args) {
// TODO: Create a ServerSocket
// TODO: In an infinite loop:
// accept an incoming client connection
// make a new thread running ClientSession for each user.
try {
ServerSocket srvSocket = new ServerSocket(5000);
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.FileMessage;
import com.university.chat.Common.MessageType;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.nio.file.Files;
public class ClientSession implements Runnable {
private String username;
private ObjectOutputStream out;
private ObjectInputStream in;
private Socket socket;
private UserManager userManage;
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()
// and an ObjectInputStream from socket.getInputStream().
}
@@ -20,6 +41,24 @@ public class ClientSession implements Runnable {
public void run() {
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)
// 1. Read the first object sent by the client.
// 2. Check it's a ChatMessage with type LOGIN.