forked from AdvancedProgramming1404/HW-10-Socket-Programming
implementing chatClient.java
This commit is contained in:
@@ -1,29 +1,108 @@
|
|||||||
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.IOException;
|
||||||
try {
|
import java.io.ObjectInputStream;
|
||||||
// TODO: Program loop — read a line from the console and act on it:
|
import java.io.ObjectOutputStream;
|
||||||
// - "/msg <user> <text>" -> build & send a PRIVATE_MESSAGE
|
import java.net.Socket;
|
||||||
// - "/users" -> build & send a USER_LIST request
|
import java.nio.file.Files;
|
||||||
// - "/sendfile <user> <path>" -> read the file into a byte[]
|
import java.nio.file.Path;
|
||||||
// (you can use TransferProgress
|
import java.nio.file.Paths;
|
||||||
// to show progress)
|
import java.util.Scanner;
|
||||||
// and send it as a FileMessage
|
|
||||||
// - anything else -> send a PUBLIC_MESSAGE
|
public class chatClient {
|
||||||
// Remember to flush() the output stream after writeObject().
|
|
||||||
} catch (Exception e){
|
public static void main() {
|
||||||
System.out.println("command failed: " + e.getMessage());
|
|
||||||
|
final String SERVER_HOST = "localhost";
|
||||||
|
final int SERVER_PORT = 600;
|
||||||
|
|
||||||
|
Scanner input = new Scanner(System.in);
|
||||||
|
|
||||||
|
try {
|
||||||
|
Socket clientSocket = new Socket(SERVER_HOST, SERVER_PORT);
|
||||||
|
|
||||||
|
ObjectOutputStream output = new ObjectOutputStream(clientSocket.getOutputStream());
|
||||||
|
output.flush();
|
||||||
|
ObjectInputStream inputStream = new ObjectInputStream(clientSocket.getInputStream());
|
||||||
|
|
||||||
|
System.out.print("Username: ");
|
||||||
|
String username = input.nextLine();
|
||||||
|
|
||||||
|
output.writeObject(new ChatMessage(MessageType.LOGIN, username, null, null));
|
||||||
|
output.flush();
|
||||||
|
|
||||||
|
Thread serverThread = new Thread(new ServerListener(inputStream));
|
||||||
|
serverThread.start();
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
try {
|
||||||
|
String command = input.nextLine();
|
||||||
|
|
||||||
|
if (command.startsWith("/msg ")) {
|
||||||
|
String[] args = command.substring(5).split(" ", 2);
|
||||||
|
|
||||||
|
String recipient = args[0];
|
||||||
|
String message = args.length > 1 ? args[1] : "";
|
||||||
|
|
||||||
|
output.writeObject(new ChatMessage(
|
||||||
|
MessageType.PRIVATE_MESSAGE,
|
||||||
|
username,
|
||||||
|
recipient,
|
||||||
|
message));
|
||||||
|
|
||||||
|
output.flush();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (command.equals("/users")) {
|
||||||
|
output.writeObject(new ChatMessage(
|
||||||
|
MessageType.USER_LIST,
|
||||||
|
username,
|
||||||
|
null,
|
||||||
|
null));
|
||||||
|
|
||||||
|
output.flush();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (command.startsWith("/sendfile ")) {
|
||||||
|
String[] args = command.substring(10).split(" ", 2);
|
||||||
|
|
||||||
|
String recipient = args[0];
|
||||||
|
Path file = Paths.get(args[1]);
|
||||||
|
|
||||||
|
byte[] fileData = Files.readAllBytes(file);
|
||||||
|
String fileName = file.getFileName().toString();
|
||||||
|
|
||||||
|
output.writeObject(new FileMessage(
|
||||||
|
username,
|
||||||
|
recipient,
|
||||||
|
fileName,
|
||||||
|
fileData));
|
||||||
|
|
||||||
|
output.flush();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
output.writeObject(new ChatMessage(
|
||||||
|
MessageType.PUBLIC_MESSAGE,
|
||||||
|
username,
|
||||||
|
null,
|
||||||
|
command));
|
||||||
|
|
||||||
|
output.flush();
|
||||||
|
|
||||||
|
} catch (Exception ex) {
|
||||||
|
System.out.println("Command failed: " + ex.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} catch (IOException ex) {
|
||||||
|
System.out.println(ex.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user