Develop #1
@@ -1,29 +1,106 @@
|
|||||||
package com.university.chat.Client;
|
package com.university.chat.Client;
|
||||||
|
|
||||||
public class chatClient {
|
import com.university.chat.Common.ChatMessage;
|
||||||
public static void main() {
|
import com.university.chat.Common.FileMessage;
|
||||||
// TODO: Connecting to the server
|
import com.university.chat.Common.MessageType;
|
||||||
// 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){
|
import java.io.File;
|
||||||
|
import java.io.ObjectInputStream;
|
||||||
|
import java.io.ObjectOutputStream;
|
||||||
|
import java.net.Socket;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class chatClient {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
try {
|
try {
|
||||||
// TODO: Program loop — read a line from the console and act on it:
|
Socket socket = new Socket("localhost", 5000);
|
||||||
// - "/msg <user> <text>" -> build & send a PRIVATE_MESSAGE
|
|
||||||
// - "/users" -> build & send a USER_LIST request
|
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
|
||||||
// - "/sendfile <user> <path>" -> read the file into a byte[]
|
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
|
||||||
// (you can use TransferProgress
|
|
||||||
// to show progress)
|
Scanner scanner = new Scanner(System.in);
|
||||||
// and send it as a FileMessage
|
|
||||||
// - anything else -> send a PUBLIC_MESSAGE
|
System.out.print("Enter username: ");
|
||||||
// Remember to flush() the output stream after writeObject().
|
String username = scanner.nextLine();
|
||||||
} catch (Exception e){
|
|
||||||
|
ChatMessage loginMessage = new ChatMessage(
|
||||||
|
MessageType.LOGIN,
|
||||||
|
username,
|
||||||
|
null,
|
||||||
|
""
|
||||||
|
);
|
||||||
|
|
||||||
|
out.writeObject(loginMessage);
|
||||||
|
out.flush();
|
||||||
|
|
||||||
|
new Thread(new ServerListener(in)).start();
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
try {
|
||||||
|
String line = scanner.nextLine();
|
||||||
|
|
||||||
|
if (line.startsWith("/msg ")) {
|
||||||
|
String[] parts = line.split(" ", 3);
|
||||||
|
|
||||||
|
ChatMessage privateMessage = new ChatMessage(
|
||||||
|
MessageType.PRIVATE_MESSAGE,
|
||||||
|
username,
|
||||||
|
parts[1],
|
||||||
|
parts[2]
|
||||||
|
);
|
||||||
|
|
||||||
|
out.writeObject(privateMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (line.equals("/users")) {
|
||||||
|
ChatMessage userListMessage = new ChatMessage(
|
||||||
|
MessageType.USER_LIST,
|
||||||
|
username,
|
||||||
|
null,
|
||||||
|
""
|
||||||
|
);
|
||||||
|
|
||||||
|
out.writeObject(userListMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (line.startsWith("/sendfile ")) {
|
||||||
|
String[] parts = line.split(" ", 3);
|
||||||
|
|
||||||
|
File file = new File(parts[2]);
|
||||||
|
byte[] data = Files.readAllBytes(file.toPath());
|
||||||
|
|
||||||
|
FileMessage fileMessage = new FileMessage(
|
||||||
|
username,
|
||||||
|
parts[1],
|
||||||
|
file.getName(),
|
||||||
|
data
|
||||||
|
);
|
||||||
|
|
||||||
|
out.writeObject(fileMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
else {
|
||||||
|
ChatMessage publicMessage = new ChatMessage(
|
||||||
|
MessageType.PUBLIC_MESSAGE,
|
||||||
|
username,
|
||||||
|
null,
|
||||||
|
line
|
||||||
|
);
|
||||||
|
|
||||||
|
out.writeObject(publicMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
out.flush();
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
System.out.println("command failed: " + e.getMessage());
|
System.out.println("command failed: " + e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println("Could not connect to server");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user