correcting chatClient
This commit is contained in:
@@ -1,64 +1,135 @@
|
|||||||
package com.university.chat.Client;
|
package com.university.chat.Client;
|
||||||
|
|
||||||
import com.university.chat.Common.ChatMessage;
|
import com.university.chat.Common.ChatMessage;
|
||||||
import com.university.chat.Server.ClientSession;
|
import com.university.chat.Common.FileMessage;
|
||||||
import com.university.chat.Server.UserManager;
|
import com.university.chat.Common.MessageType;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.File;
|
||||||
import java.io.ObjectInputStream;
|
import java.io.ObjectInputStream;
|
||||||
import java.io.ObjectOutputStream;
|
import java.io.ObjectOutputStream;
|
||||||
import java.net.ServerSocket;
|
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
import java.nio.file.Files;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class chatClient {
|
public class chatClient {
|
||||||
public static final UserManager userManager =
|
|
||||||
new UserManager();
|
|
||||||
|
|
||||||
public static void main() {
|
public static void main(String[] args) {
|
||||||
// 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;
|
|
||||||
|
|
||||||
try{
|
|
||||||
serverSocket =new ServerSocket(5000);
|
|
||||||
|
|
||||||
System.out.println("Server started");
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
while (true){
|
|
||||||
try {
|
try {
|
||||||
// TODO: Program loop — read a line from the console and act on it:
|
Socket socket = new Socket("localhost", 8080);
|
||||||
// - "/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().
|
|
||||||
|
|
||||||
Socket socket =
|
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
|
||||||
serverSocket.accept();
|
|
||||||
|
|
||||||
ClientSession session =
|
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
|
||||||
new ClientSession(
|
|
||||||
socket,
|
Scanner scanner = new Scanner(System.in);
|
||||||
userManager
|
|
||||||
|
System.out.print("Enter username: ");
|
||||||
|
String username = scanner.nextLine();
|
||||||
|
|
||||||
|
ChatMessage login = new ChatMessage(
|
||||||
|
MessageType.LOGIN,
|
||||||
|
username,
|
||||||
|
"",
|
||||||
|
""
|
||||||
);
|
);
|
||||||
|
|
||||||
new Thread(session).start();
|
out.writeObject(login);
|
||||||
|
|
||||||
} catch (Exception e){
|
new Thread(new ServerListener(in)).start();
|
||||||
System.out.println("command failed: " + e.getMessage());
|
|
||||||
|
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]
|
||||||
|
);
|
||||||
|
|
||||||
|
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) {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -10,7 +10,6 @@ import java.io.ObjectOutputStream;
|
|||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
|
|
||||||
import static com.university.chat.Client.chatClient.userManager;
|
|
||||||
|
|
||||||
public class ClientSession implements Runnable {
|
public class ClientSession implements Runnable {
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user