forked from AdvancedProgramming1404/HW-10-Socket-Programming
127 lines
6.2 KiB
Java
127 lines
6.2 KiB
Java
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.util.Scanner;
|
|
|
|
public class chatClient {
|
|
private static final String DEFAULT_HOST = "localhost";
|
|
private static final int DEFAULT_PORT = 8585;
|
|
public static void main(String[] args) {
|
|
// TODO: Connecting to the server
|
|
// 1. Create a socket and connect to the server
|
|
Scanner scanner = new Scanner(System.in);
|
|
System.out.print("Enter username: ");
|
|
String username = scanner.nextLine().trim();
|
|
try {
|
|
Socket socket = new Socket(DEFAULT_HOST, DEFAULT_PORT);
|
|
// 2. Create an ObjectOutputStream (out) and ObjectInputStream (in)
|
|
// from the socket's streams — output FIRST, then input.
|
|
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
|
|
out.flush();
|
|
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
|
|
// 2. Get the username, and send a LOGIN ChatMessage with that username
|
|
out.writeObject(new ChatMessage(MessageType.LOGIN, username, "SERVER", ""));
|
|
out.flush();
|
|
Object response = in.readObject();
|
|
if (response instanceof ChatMessage serverMsg){
|
|
if (serverMsg.getType() == MessageType.LOGIN_FAILED){
|
|
System.out.println("Login failed: " + serverMsg.getContent());
|
|
socket.close();
|
|
scanner.close();
|
|
return;
|
|
}
|
|
else if (serverMsg.getType() == MessageType.LOGIN_SUCCESS){
|
|
System.out.println("Login successful! " + serverMsg.getContent());
|
|
System.out.println("Commands: /msg <user> <text>, /users, /sendfile <user> <path>, or just type text for public chat.");
|
|
}
|
|
}
|
|
// 3. Start a new Thread running a ServerListener(in) so incoming
|
|
// messages are handled concurrently.
|
|
ServerListener listener = new ServerListener(in);
|
|
Thread listenerThread = new Thread(listener);
|
|
listenerThread.start();
|
|
|
|
while (true){
|
|
try {
|
|
// TODO: Program loop — read a line from the console and act on it:
|
|
String line = scanner.nextLine().trim();
|
|
if (line.isEmpty()) continue;
|
|
if (line.startsWith("/msg ")){
|
|
// - "/msg <user> <text>" -> build & send a PRIVATE_MESSAGE
|
|
String[] parts = line.split(" ", 3);
|
|
if (parts.length >= 3){
|
|
String targetUser = parts[1];
|
|
String text = parts[2];
|
|
out.writeObject(new ChatMessage(MessageType.PRIVATE_MESSAGE, username, targetUser, text));
|
|
out.flush();
|
|
}
|
|
else{
|
|
System.out.println("Usage: /msg <user> <text>");
|
|
}
|
|
}
|
|
else if (line.equals("/users")){
|
|
// - "/users" -> build & send a USER_LIST request
|
|
out.writeObject(new ChatMessage(MessageType.USER_LIST, username, "SERVER", ""));
|
|
out.flush();
|
|
}
|
|
else if (line.startsWith("/sendfile ")){
|
|
// - "/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
|
|
String[] parts = line.split(" ", 3);
|
|
if (parts.length >= 3){
|
|
String targetUser = parts[1];
|
|
String filePath = parts[2];
|
|
File file = new File(filePath);
|
|
if (!file.exists() || !file.isFile()){
|
|
System.out.println("File does not exist or is not a valid file.");
|
|
continue;
|
|
}
|
|
|
|
long fileSize = file.length();
|
|
byte[] fileData = new byte[(int) fileSize];
|
|
// Reading file while simulating the TransferProgress bar
|
|
try (FileInputStream fis = new FileInputStream(file)){
|
|
TransferProgress progress = new TransferProgress(fileSize);
|
|
int bytesRead;
|
|
int totalBytesRead = 0;
|
|
byte[] buffer = new byte[4096];
|
|
|
|
while ((bytesRead = fis.read(buffer)) != -1){
|
|
System.arraycopy(buffer, 0, fileData, totalBytesRead, bytesRead);
|
|
totalBytesRead += bytesRead;
|
|
progress.update(totalBytesRead);
|
|
}
|
|
}
|
|
|
|
FileMessage fileMsg = new FileMessage(username, targetUser, file.getName(), fileData);
|
|
out.writeObject(fileMsg);
|
|
out.flush();
|
|
System.out.println("File sent to server successfully.");
|
|
}
|
|
else{
|
|
System.out.println("Usage: /sendfile <user> <path>");
|
|
}
|
|
}
|
|
else{
|
|
// - anything else -> send a PUBLIC_MESSAGE
|
|
out.writeObject(new ChatMessage(MessageType.PUBLIC_MESSAGE, username, "ALL", line));
|
|
out.flush();
|
|
}
|
|
// Remember to flush() the output stream after writeObject().
|
|
}
|
|
catch (Exception e){
|
|
System.out.println("command failed: " + e.getMessage());
|
|
}
|
|
}
|
|
}
|
|
catch (Exception e){
|
|
System.out.println("Client Error: " + e.getMessage());
|
|
}
|
|
}}
|