Develop #1
@@ -17,7 +17,7 @@ public class ServerListener implements Runnable{
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
|
||||
|
||||
while (true) {
|
||||
Object obj = in.readObject();
|
||||
if (obj instanceof ChatMessage msg) {
|
||||
|
||||
@@ -1,29 +1,97 @@
|
||||
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;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.net.Socket;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
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.
|
||||
|
||||
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){
|
||||
System.out.println("command failed: " + e.getMessage());
|
||||
String host = "localhost";
|
||||
int port = 9000;
|
||||
|
||||
try {
|
||||
|
||||
Socket socket = new Socket(host, port);
|
||||
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
|
||||
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
|
||||
|
||||
Scanner sc = new Scanner(System.in);
|
||||
System.out.println("Enter your username: ");
|
||||
String username = sc.nextLine();
|
||||
|
||||
out.writeObject(new ChatMessage(MessageType.LOGIN, username, null, null));
|
||||
out.flush();
|
||||
|
||||
Object loginResponse = in.readObject();
|
||||
if (loginResponse instanceof ChatMessage response) {
|
||||
if (response.getType() == MessageType.LOGIN_FAILED) {
|
||||
System.out.println("Login failed: " + response.getContent());
|
||||
socket.close();
|
||||
return;
|
||||
}
|
||||
System.out.println(response.getContent());
|
||||
}
|
||||
|
||||
Thread listenerThread = new Thread(new ServerListener(in));
|
||||
listenerThread.setDaemon(true);
|
||||
listenerThread.start();
|
||||
|
||||
while (true) {
|
||||
try {
|
||||
String line = sc.nextLine();
|
||||
|
||||
if (line.startsWith("/msg ")) {
|
||||
String[] parts = line.split(" ", 3);
|
||||
if (parts.length < 3) {
|
||||
System.out.println("Usage: /msg <user> <text>");
|
||||
continue;
|
||||
}
|
||||
String target = parts[1];
|
||||
String text = parts[2];
|
||||
out.writeObject(new ChatMessage(MessageType.PRIVATE_MESSAGE, username, target, text));
|
||||
out.flush();
|
||||
|
||||
} else if (line.equals("/users")) {
|
||||
out.writeObject(new ChatMessage(MessageType.USER_LIST, username, null, null));
|
||||
out.flush();
|
||||
|
||||
} else if (line.startsWith("/sendfile ")) {
|
||||
String[] parts = line.split(" ", 3);
|
||||
if (parts.length < 3) {
|
||||
System.out.println("Usage: /sendfile <user> <filepath>");
|
||||
continue;
|
||||
}
|
||||
String target = parts[1];
|
||||
String filePath = parts[2];
|
||||
|
||||
byte[] fileData = Files.readAllBytes(Paths.get(filePath));
|
||||
String filename = Paths.get(filePath).getFileName().toString();
|
||||
|
||||
TransferProgress progress = new TransferProgress(fileData.length);
|
||||
progress.update(fileData.length);
|
||||
|
||||
out.writeObject(new FileMessage(username, target, filename, fileData));
|
||||
out.flush();
|
||||
System.out.println("File '" + filename + "' sent to " + target + ".");
|
||||
|
||||
} else {
|
||||
out.writeObject(new ChatMessage(MessageType.PUBLIC_MESSAGE, username, null, line));
|
||||
out.flush();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println("command failed: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println(" could not connect to " + host + ":" + port);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user