develop #1
@@ -1,64 +1,135 @@
|
||||
package com.university.chat.Client;
|
||||
|
||||
import com.university.chat.Common.ChatMessage;
|
||||
import com.university.chat.Server.ClientSession;
|
||||
import com.university.chat.Server.UserManager;
|
||||
import com.university.chat.Common.FileMessage;
|
||||
import com.university.chat.Common.MessageType;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.File;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.nio.file.Files;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class chatClient {
|
||||
public static final UserManager userManager =
|
||||
new UserManager();
|
||||
|
||||
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.
|
||||
ServerSocket serverSocket;
|
||||
public static void main(String[] args) {
|
||||
|
||||
try{
|
||||
serverSocket =new ServerSocket(5000);
|
||||
try {
|
||||
Socket socket = new Socket("localhost", 8080);
|
||||
|
||||
System.out.println("Server started");
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
|
||||
|
||||
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().
|
||||
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
|
||||
|
||||
Socket socket =
|
||||
serverSocket.accept();
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
|
||||
ClientSession session =
|
||||
new ClientSession(
|
||||
socket,
|
||||
userManager
|
||||
System.out.print("Enter username: ");
|
||||
String username = scanner.nextLine();
|
||||
|
||||
ChatMessage login = new ChatMessage(
|
||||
MessageType.LOGIN,
|
||||
username,
|
||||
"",
|
||||
""
|
||||
);
|
||||
|
||||
out.writeObject(login);
|
||||
|
||||
new Thread(new ServerListener(in)).start();
|
||||
|
||||
while (true) {
|
||||
|
||||
try {
|
||||
|
||||
String text = scanner.nextLine();
|
||||
|
||||
if (text.startsWith("/msg ")) {
|
||||
|
||||
String[] parts = text.split(" ", 3);
|
||||
|
||||
if (parts.length < 3) {
|
||||
System.out.println("Wrong command.");
|
||||
continue;
|
||||
}
|
||||
|
||||
ChatMessage message = new ChatMessage(
|
||||
MessageType.PRIVATE_MESSAGE,
|
||||
username,
|
||||
parts[1],
|
||||
parts[2]
|
||||
);
|
||||
|
||||
new Thread(session).start();
|
||||
out.writeObject(message);
|
||||
out.flush();
|
||||
}
|
||||
|
||||
else if (text.equals("/users")) {
|
||||
|
||||
ChatMessage message = new ChatMessage(
|
||||
MessageType.USER_LIST,
|
||||
username,
|
||||
"",
|
||||
""
|
||||
);
|
||||
|
||||
out.writeObject(message);
|
||||
out.flush();
|
||||
}
|
||||
|
||||
else if (text.startsWith("/sendfile ")) {
|
||||
|
||||
String[] parts = text.split(" ", 3);
|
||||
|
||||
if (parts.length < 3) {
|
||||
System.out.println("Wrong command.");
|
||||
continue;
|
||||
}
|
||||
|
||||
File file = new File(parts[2]);
|
||||
|
||||
if (!file.exists()) {
|
||||
System.out.println("File not found.");
|
||||
continue;
|
||||
}
|
||||
|
||||
byte[] data = Files.readAllBytes(file.toPath());
|
||||
|
||||
FileMessage fileMessage = new FileMessage(
|
||||
username,
|
||||
parts[1],
|
||||
file.getName(),
|
||||
data
|
||||
);
|
||||
|
||||
out.writeObject(fileMessage);
|
||||
out.flush();
|
||||
|
||||
System.out.println("File sent.");
|
||||
}
|
||||
|
||||
else {
|
||||
|
||||
ChatMessage message = new ChatMessage(
|
||||
MessageType.PUBLIC_MESSAGE,
|
||||
username,
|
||||
"",
|
||||
text
|
||||
);
|
||||
|
||||
out.writeObject(message);
|
||||
out.flush();
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
System.out.println("Command failed.");
|
||||
}
|
||||
|
||||
} catch (Exception e){
|
||||
System.out.println("command failed: " + e.getMessage());
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -10,7 +10,6 @@ import java.io.ObjectOutputStream;
|
||||
import java.net.Socket;
|
||||
import java.nio.file.Files;
|
||||
|
||||
import static com.university.chat.Client.chatClient.userManager;
|
||||
|
||||
public class ClientSession implements Runnable {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user