From 866d8532fa70612ed2d3ba0943121e246020b4f4 Mon Sep 17 00:00:00 2001 From: jmakho01 <210610870+jmakho01@users.noreply.github.com> Date: Sat, 6 Dec 2025 20:42:09 -0800 Subject: [PATCH 1/9] Implemented parseArgs method (Wave 2) --- src/ChatterboxClient.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/ChatterboxClient.java b/src/ChatterboxClient.java index 9f80fc0..c8e1c5e 100644 --- a/src/ChatterboxClient.java +++ b/src/ChatterboxClient.java @@ -125,9 +125,10 @@ public static void main(String[] args) { * @throws IllegalArgumentException on any bad/missing input */ public static ChatterboxOptions parseArgs(String[] args) throws IllegalArgumentException { - // TODO: read args in the required order and return new ChatterboxOptions(host, port, username, password) - // Remove this exception - throw new UnsupportedOperationException("Argument parsing not yet implemented. Implement parseArgs and remove this exception"); + if (args.length != 4) throw new IllegalArgumentException("Argument length invalid"); + int portCheck = Integer.parseInt(args[1]); + if (portCheck < 1 || portCheck > 65535) throw new IllegalArgumentException("Port number invalid"); + return new ChatterboxOptions(args[0], portCheck, args[2], args[3]); } /** From 869a3756d577bfbb1b4e70de94b0c2d2fb6b7457 Mon Sep 17 00:00:00 2001 From: jmakho01 <210610870+jmakho01@users.noreply.github.com> Date: Sat, 6 Dec 2025 20:48:39 -0800 Subject: [PATCH 2/9] Implemented ChatterboxClient constructor (Wave 3) --- src/ChatterboxClient.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/ChatterboxClient.java b/src/ChatterboxClient.java index c8e1c5e..d46ed18 100644 --- a/src/ChatterboxClient.java +++ b/src/ChatterboxClient.java @@ -48,7 +48,7 @@ public class ChatterboxClient { * Example: * javac src/*.java && java -cp src ChatterboxClient localhost 12345 sharon abc123 * - * This method is already complete. Your work is in the TODO methods below. + * This method is already complete. Your work is in the other methods below. */ public static void main(String[] args) { ChatterboxOptions options = null; @@ -147,8 +147,10 @@ public ChatterboxClient(ChatterboxOptions options, InputStream userInput, Output this.userInput = new Scanner(userInput, StandardCharsets.UTF_8); this.userOutput = userOutput; - throw new UnsupportedOperationException("Constructor not yet implemented. Implement ChatterboxClient constructor and remove this exception"); - // TODO: copy options.getHost(), getPort(), getUsername(), getPassword() into fields + this.host = options.getHost(); + this.port = options.getPort(); + this.username = options.getUsername(); + this.password = options.getPassword(); } /** From 2fc6a78cc14e088763e73342596f6e6e9e1d819c Mon Sep 17 00:00:00 2001 From: jmakho01 <210610870+jmakho01@users.noreply.github.com> Date: Sat, 6 Dec 2025 21:04:42 -0800 Subject: [PATCH 3/9] Implemented connect method (Wave 4) --- src/ChatterboxClient.java | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/ChatterboxClient.java b/src/ChatterboxClient.java index d46ed18..0258e55 100644 --- a/src/ChatterboxClient.java +++ b/src/ChatterboxClient.java @@ -2,7 +2,10 @@ import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.net.Socket; import java.nio.charset.StandardCharsets; import java.util.Scanner; @@ -167,9 +170,21 @@ public ChatterboxClient(ChatterboxOptions options, InputStream userInput, Output * @throws IOException if the socket cannot be opened */ public void connect() throws IOException { - throw new UnsupportedOperationException("Connect not yet implemented. Implement connect() and remove this exception!"); + Socket socket = new Socket(host, port); + + try { + InputStream inputStream = socket.getInputStream(); + InputStreamReader inputStreamReader = new InputStreamReader(inputStream, java.nio.charset.StandardCharsets.UTF_8); + this.serverReader = new BufferedReader(inputStreamReader); + + OutputStream outputStream = socket.getOutputStream(); + OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, java.nio.charset.StandardCharsets.UTF_8); + this.serverWriter = new BufferedWriter(outputStreamWriter); + } catch (IOException e) { + System.err.println("Error reading from socket"); + System.err.println(e.getMessage()); + } - // Make sure to have this.serverReader and this.serverWriter set by the end of this method! // hint: get the streams from the sockets, use those to create the InputStreamReader/OutputStreamWriter and the BufferedReader/BufferedWriter } From 135644ad0db60ff0fa2083dd64f325f40e06784f Mon Sep 17 00:00:00 2001 From: jmakho01 <210610870+jmakho01@users.noreply.github.com> Date: Sat, 6 Dec 2025 23:10:47 -0800 Subject: [PATCH 4/9] Implemented authenticate method (Wave 5) --- src/ChatterboxClient.java | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/ChatterboxClient.java b/src/ChatterboxClient.java index 0258e55..f5de15a 100644 --- a/src/ChatterboxClient.java +++ b/src/ChatterboxClient.java @@ -181,7 +181,7 @@ public void connect() throws IOException { OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, java.nio.charset.StandardCharsets.UTF_8); this.serverWriter = new BufferedWriter(outputStreamWriter); } catch (IOException e) { - System.err.println("Error reading from socket"); + System.err.println("Error opening socket"); System.err.println(e.getMessage()); } @@ -196,7 +196,7 @@ public void connect() throws IOException { * to userOutput. * - Send ONE LINE containing: * username + " " + password + "\n" - * using serverOutput. + * using serverWriter. * - Read ONE response line from serverReader. * - If the response indicates failure, throw IllegalArgumentException * with that response text. @@ -205,13 +205,27 @@ public void connect() throws IOException { * Assumption: * - The server closes the connection after a failed auth. * - * @throws IOException for network errors + * @throws IOException for network error(s) * @throws IllegalArgumentException for bad credentials / server rejection */ public void authenticate() throws IOException, IllegalArgumentException { - throw new UnsupportedOperationException("Authenticate not yet implemented. Implement authenticate() and remove this exception!"); - // Hint: use the username/password instance variables, DO NOT READ FROM userInput - // send messages using serverWriter (don't forget to flush!) + String line = serverReader.readLine(); + if (line != null) { + userOutput.write(line.getBytes()); + } + + userOutput.write((getUsername() + " " + getPassword()).getBytes()); + serverWriter.write(getUsername() + " " + getPassword()); + serverWriter.newLine(); + serverWriter.flush(); + + userOutput.write(10); // 10 represents a newline in ASCII + + line = serverReader.readLine(); + if (line.contains("fail")) throw new IllegalArgumentException(line); + userOutput.write(line.getBytes()); + userOutput.write(10); + return; } /** From fe22a7d754a141518df55c6b3271c0dc3bd17fbe Mon Sep 17 00:00:00 2001 From: jmakho01 <210610870+jmakho01@users.noreply.github.com> Date: Sat, 6 Dec 2025 23:31:29 -0800 Subject: [PATCH 5/9] Implemented printIncomingChats method (Wave 6) --- src/ChatterboxClient.java | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/ChatterboxClient.java b/src/ChatterboxClient.java index f5de15a..96cef81 100644 --- a/src/ChatterboxClient.java +++ b/src/ChatterboxClient.java @@ -184,8 +184,6 @@ public void connect() throws IOException { System.err.println("Error opening socket"); System.err.println(e.getMessage()); } - - // hint: get the streams from the sockets, use those to create the InputStreamReader/OutputStreamWriter and the BufferedReader/BufferedWriter } /** @@ -223,6 +221,10 @@ public void authenticate() throws IOException, IllegalArgumentException { line = serverReader.readLine(); if (line.contains("fail")) throw new IllegalArgumentException(line); + + userOutput.write(line.getBytes()); + userOutput.write(10); + line = serverReader.readLine(); userOutput.write(line.getBytes()); userOutput.write(10); return; @@ -241,7 +243,7 @@ public void authenticate() throws IOException, IllegalArgumentException { * @throws IOException */ public void streamChat() throws IOException { - throw new UnsupportedOperationException("Chat streaming not yet implemented. Implement streamChat() and remove this exception!"); + printIncomingChats(); } /** @@ -259,8 +261,17 @@ public void streamChat() throws IOException { * print a message to userOutput and exit. */ public void printIncomingChats() { - // Listen on serverReader - // Write to userOutput, NOT System.out + try { + String line; + while((line = serverReader.readLine()) != null) { + userOutput.write(line.getBytes()); + userOutput.write(10); + } + } catch (IOException e) { + System.err.println("Disconnected from server"); + System.err.println(e.getMessage()); + System.exit(1); + } } /** From 113ab15e293bb05f74a6c13de36203bfd6247590 Mon Sep 17 00:00:00 2001 From: jmakho01 <210610870+jmakho01@users.noreply.github.com> Date: Sun, 7 Dec 2025 00:35:42 -0800 Subject: [PATCH 6/9] Implemented printIncomingChats method (Wave 6) --- src/ChatterboxClient.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/ChatterboxClient.java b/src/ChatterboxClient.java index 96cef81..1725144 100644 --- a/src/ChatterboxClient.java +++ b/src/ChatterboxClient.java @@ -9,6 +9,8 @@ import java.nio.charset.StandardCharsets; import java.util.Scanner; +import javax.imageio.IIOException; + /** * A simple command-line chat client for the Chatterbox server. * @@ -268,9 +270,12 @@ public void printIncomingChats() { userOutput.write(10); } } catch (IOException e) { - System.err.println("Disconnected from server"); - System.err.println(e.getMessage()); - System.exit(1); + try { + userOutput.write(("Disconnected from server").getBytes()); + System.exit(1); + } catch (IOException x) { + System.err.println(x.getMessage()); + } } } From 0538ce122f35e85f44cdf42586fb586f10ea462d Mon Sep 17 00:00:00 2001 From: jmakho01 <210610870+jmakho01@users.noreply.github.com> Date: Sun, 7 Dec 2025 00:36:49 -0800 Subject: [PATCH 7/9] Implemented sendOutgoingChats method (Wave 7) --- src/ChatterboxClient.java | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/ChatterboxClient.java b/src/ChatterboxClient.java index 1725144..4f93b06 100644 --- a/src/ChatterboxClient.java +++ b/src/ChatterboxClient.java @@ -245,7 +245,11 @@ public void authenticate() throws IOException, IllegalArgumentException { * @throws IOException */ public void streamChat() throws IOException { - printIncomingChats(); + Thread incoming = new Thread(() -> printIncomingChats()); + Thread outgoing = new Thread(() -> sendOutgoingChats()); + + incoming.start(); + outgoing.start(); } /** @@ -292,9 +296,22 @@ public void printIncomingChats() { * print a message to userOutput and exit. */ public void sendOutgoingChats() { - // Use the userInput to read, NOT System.in directly - // loop forever reading user input - // write to serverOutput + while(userInput.hasNextLine()) + { + try { + String input = userInput.nextLine(); + serverWriter.write(input); + serverWriter.newLine(); + serverWriter.flush(); + } catch (IOException e) { + try { + userOutput.write(("Disconnected from server").getBytes()); + System.exit(1); + } catch (IOException x) { + System.err.println(x.getMessage()); + } + } + } } public String getHost() { From 58460afdfe58946ab329736395422c50ff7f7f96 Mon Sep 17 00:00:00 2001 From: jmakho01 <210610870+jmakho01@users.noreply.github.com> Date: Sun, 7 Dec 2025 00:44:19 -0800 Subject: [PATCH 8/9] Minor code cleanup --- src/ChatterboxClient.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/ChatterboxClient.java b/src/ChatterboxClient.java index 4f93b06..1ad4d07 100644 --- a/src/ChatterboxClient.java +++ b/src/ChatterboxClient.java @@ -9,8 +9,6 @@ import java.nio.charset.StandardCharsets; import java.util.Scanner; -import javax.imageio.IIOException; - /** * A simple command-line chat client for the Chatterbox server. * @@ -205,7 +203,7 @@ public void connect() throws IOException { * Assumption: * - The server closes the connection after a failed auth. * - * @throws IOException for network error(s) + * @throws IOException for network errors * @throws IllegalArgumentException for bad credentials / server rejection */ public void authenticate() throws IOException, IllegalArgumentException { From 5a40755c93b533eea61dfc319ee7dde8ea42d405 Mon Sep 17 00:00:00 2001 From: jmakho01 <210610870+jmakho01@users.noreply.github.com> Date: Tue, 6 Jan 2026 10:01:29 -0800 Subject: [PATCH 9/9] Added file to .gitignore --- .gitignore | 2 +- my_passwords.txt | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) delete mode 100644 my_passwords.txt diff --git a/.gitignore b/.gitignore index 36d1f49..4e9d446 100644 --- a/.gitignore +++ b/.gitignore @@ -23,4 +23,4 @@ hs_err_pid* replay_pid* -my_passwords.txt \ No newline at end of file +mypasswords.txt \ No newline at end of file diff --git a/my_passwords.txt b/my_passwords.txt deleted file mode 100644 index 0332ee1..0000000 --- a/my_passwords.txt +++ /dev/null @@ -1,2 +0,0 @@ -You can add your passwords here and they won't be committed to your git repository. -