Complete network chat system with file sharing
This commit is contained in:
@@ -1,18 +1,54 @@
|
||||
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 final ObjectInputStream in;
|
||||
|
||||
public ServerListener(ObjectInputStream in) {
|
||||
this.in = in;
|
||||
}
|
||||
|
||||
|
||||
@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 obj = in.readObject();
|
||||
if (obj instanceof ChatMessage) {
|
||||
ChatMessage msg = (ChatMessage) obj;
|
||||
switch (msg.getType()) {
|
||||
case LOGIN_SUCCESS:
|
||||
System.out.println("\n[Server] " + msg.getContent());
|
||||
break;
|
||||
case LOGIN_FAILED:
|
||||
System.out.println("\n[Server] " + msg.getContent());
|
||||
System.exit(0);
|
||||
break;
|
||||
case PUBLIC_MESSAGE:
|
||||
System.out.println("\n[" + msg.getSender() + "]: " + msg.getContent());
|
||||
break;
|
||||
case PRIVATE_MESSAGE:
|
||||
System.out.println("\n[Private from " + msg.getSender() + "]: " + msg.getContent());
|
||||
break;
|
||||
case USER_LIST:
|
||||
System.out.println("\n[Online users]: " + msg.getContent());
|
||||
break;
|
||||
default:
|
||||
System.out.println("\n[Server] " + msg.getContent());
|
||||
}
|
||||
System.out.print("> ");
|
||||
} else if (obj instanceof FileMessage) {
|
||||
FileMessage fileMsg = (FileMessage) obj;
|
||||
System.out.println("\n[File received] from " + fileMsg.getSender() +
|
||||
": " + fileMsg.getFilename() + " (saved to server_data)");
|
||||
System.out.print("> ");
|
||||
}
|
||||
}
|
||||
} catch (Exception e){
|
||||
System.out.println("Disconnected from server");
|
||||
}
|
||||
|
||||
@@ -1,29 +1,112 @@
|
||||
package com.university.chat.Client;
|
||||
|
||||
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.
|
||||
import com.university.chat.Common.ChatMessage;
|
||||
import com.university.chat.Common.FileMessage;
|
||||
import com.university.chat.Common.MessageType;
|
||||
|
||||
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().
|
||||
} catch (Exception e){
|
||||
import java.io.*;
|
||||
import java.net.Socket;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class chatClient {
|
||||
|
||||
private static ObjectOutputStream out;
|
||||
private static ObjectInputStream in;
|
||||
private static String username;
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
|
||||
Socket socket = new Socket("localhost", 12345);
|
||||
|
||||
out = new ObjectOutputStream(socket.getOutputStream());
|
||||
in = new ObjectInputStream(socket.getInputStream());
|
||||
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
System.out.print("Enter your username: ");
|
||||
username = scanner.nextLine().trim();
|
||||
|
||||
ChatMessage loginMsg = new ChatMessage(MessageType.LOGIN, username, null, "");
|
||||
out.writeObject(loginMsg);
|
||||
out.flush();
|
||||
|
||||
|
||||
ServerListener listener = new ServerListener(in);
|
||||
new Thread(listener).start();
|
||||
while (true){
|
||||
try {
|
||||
System.out.print("> ");
|
||||
String line = scanner.nextLine();
|
||||
if (line == null) break;
|
||||
|
||||
if (line.equals("/exit") || line.equals("/quit")) {
|
||||
System.out.println("Disconnecting...");
|
||||
break;
|
||||
}
|
||||
|
||||
if (line.startsWith("/msg ")) {
|
||||
String[] parts = line.split(" ", 3);
|
||||
if (parts.length < 3) {
|
||||
System.out.println("Usage: /msg <username> <message>");
|
||||
continue;
|
||||
}
|
||||
String target = parts[1];
|
||||
String content = parts[2];
|
||||
ChatMessage privateMsg = new ChatMessage(MessageType.PRIVATE_MESSAGE,
|
||||
username, target, content);
|
||||
out.writeObject(privateMsg);
|
||||
out.flush();
|
||||
} else if (line.equals("/users")) {
|
||||
ChatMessage userListReq = new ChatMessage(MessageType.USER_LIST,
|
||||
username, null, "");
|
||||
out.writeObject(userListReq);
|
||||
out.flush();
|
||||
} else if (line.startsWith("/sendfile ")) {
|
||||
String[] parts = line.split(" ", 3);
|
||||
if (parts.length < 3) {
|
||||
System.out.println("Usage: /sendfile <username> <filepath>");
|
||||
continue;
|
||||
}
|
||||
String target = parts[1];
|
||||
String filePath = parts[2];
|
||||
|
||||
Path path = Paths.get(filePath);
|
||||
if (!Files.exists(path) || Files.isDirectory(path)) {
|
||||
System.out.println("File does not exist or is a directory.");
|
||||
continue;
|
||||
}
|
||||
|
||||
byte[] data = Files.readAllBytes(path);
|
||||
String filename = path.getFileName().toString();
|
||||
|
||||
TransferProgress progress = new TransferProgress(data.length);
|
||||
progress.update(data.length);
|
||||
|
||||
FileMessage fileMsg = new FileMessage(username, target, filename, data);
|
||||
out.writeObject(fileMsg);
|
||||
out.flush();
|
||||
System.out.println("File sent to " + target);
|
||||
} else {
|
||||
ChatMessage publicMsg = new ChatMessage(MessageType.PUBLIC_MESSAGE,
|
||||
username, null, line);
|
||||
out.writeObject(publicMsg);
|
||||
out.flush();
|
||||
}
|
||||
}catch (IOException e) {
|
||||
System.out.println("Connection lost. Exiting...");
|
||||
break;
|
||||
} catch (Exception e){
|
||||
System.out.println("command failed: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
socket.close();
|
||||
scanner.close();
|
||||
} catch (Exception e) {
|
||||
System.out.println("Connection error: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user