From 9eb0329ac5ac0daab99f145abea8cec702314553 Mon Sep 17 00:00:00 2001 From: Fredrick_Karau <30944677+Karau1218@users.noreply.github.com> Date: Fri, 5 Dec 2025 19:39:37 -0800 Subject: [PATCH 01/15] working on wave 2 --- src/ChatterboxClient.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/ChatterboxClient.java b/src/ChatterboxClient.java index 9f80fc0..b3ce7fe 100644 --- a/src/ChatterboxClient.java +++ b/src/ChatterboxClient.java @@ -6,6 +6,10 @@ import java.nio.charset.StandardCharsets; import java.util.Scanner; + +WORKING ON FILE NOW: REMOVE LATER -- REMEMBER TO REMOVE THIS LINE: + + /** * A simple command-line chat client for the Chatterbox server. * From 95e60f195678ae96b428172ea22ab8ffe43b432f Mon Sep 17 00:00:00 2001 From: Fkarau <30944677+Karau1218@users.noreply.github.com> Date: Sun, 7 Dec 2025 00:07:27 -0800 Subject: [PATCH 02/15] done w wave 2 --- src/ChatterboxClient.java | 40 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/src/ChatterboxClient.java b/src/ChatterboxClient.java index b3ce7fe..1f7e280 100644 --- a/src/ChatterboxClient.java +++ b/src/ChatterboxClient.java @@ -7,7 +7,7 @@ import java.util.Scanner; -WORKING ON FILE NOW: REMOVE LATER -- REMEMBER TO REMOVE THIS LINE: +// WORKING ON FILE NOW: REMOVE LATER -- REMEMBER TO REMOVE THIS LINE: /** @@ -131,9 +131,32 @@ public static void main(String[] args) { 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"); + // throw new UnsupportedOperationException("Argument parsing not yet implemented. Implement parseArgs and remove this exception"); + if ( args.length != 4) { + throw new IllegalArgumentException("Expected 4 arguments: HOST PORT USERNAME PASSWORD"); + + } + String host = args[0]; + String portString = args[1]; + String username = args[2]; + String password = args[3]; + + int port; + try { + port = Integer.parseInt(portString); + } catch (NumberFormatException e) { + throw new IllegalArgumentException("Port must be a valid integer."); } + if (port < 1 || port > 65535) { + throw new IllegalArgumentException("Port must be between 1 and 65535."); + } + + return new ChatterboxOptions(host, port, username, password); +} + + + /** * Construct a ChatterboxClient from already-parsed options and user streams. * @@ -152,7 +175,18 @@ public ChatterboxClient(ChatterboxOptions options, InputStream userInput, Output throw new UnsupportedOperationException("Constructor not yet implemented. Implement ChatterboxClient constructor and remove this exception"); // TODO: copy options.getHost(), getPort(), getUsername(), getPassword() into fields - } + // Save user I/O streams + this.userInput = new Scanner(userInput, StandardCharsets.UTF_8); + this.userOutput = userOutput; + + // Copy option fields into instance variables + this.host = options.getHost(); + this.port = options.getPort(); + this.username = options.getUsername(); + this.password = options.getPassword(); +} + + /** * Open a TCP connection to the server. From ad651b7f20f114e339f9c54f266e10fe01f4e93f Mon Sep 17 00:00:00 2001 From: Fkarau <30944677+Karau1218@users.noreply.github.com> Date: Sun, 7 Dec 2025 00:27:28 -0800 Subject: [PATCH 03/15] modified chatterboxoptions & added chatterboxclient constructor --- src/ChatterboxClient.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/ChatterboxClient.java b/src/ChatterboxClient.java index 1f7e280..2d3aaa0 100644 --- a/src/ChatterboxClient.java +++ b/src/ChatterboxClient.java @@ -173,13 +173,11 @@ 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"); + // NOTE: add // on line 177 --> so it removes the red scuiggle line in line 181 + // throw new UnsupportedOperationException("Constructor not yet implemented. Implement ChatterboxClient constructor and remove this exception"); // TODO: copy options.getHost(), getPort(), getUsername(), getPassword() into fields - // Save user I/O streams - this.userInput = new Scanner(userInput, StandardCharsets.UTF_8); - this.userOutput = userOutput; + - // Copy option fields into instance variables this.host = options.getHost(); this.port = options.getPort(); this.username = options.getUsername(); From 85dd542f463f8c1d0790e7fe3295cad747d77392 Mon Sep 17 00:00:00 2001 From: Fkarau <30944677+Karau1218@users.noreply.github.com> Date: Sun, 7 Dec 2025 01:06:31 -0800 Subject: [PATCH 04/15] added WAVE connect 4 Input reader and output writer --- src/ChatterboxClient.java | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/ChatterboxClient.java b/src/ChatterboxClient.java index 2d3aaa0..7a95f97 100644 --- a/src/ChatterboxClient.java +++ b/src/ChatterboxClient.java @@ -6,6 +6,10 @@ import java.nio.charset.StandardCharsets; import java.util.Scanner; +import java.net.Socket; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; + // WORKING ON FILE NOW: REMOVE LATER -- REMEMBER TO REMOVE THIS LINE: @@ -200,11 +204,26 @@ 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!"); + // throw new UnsupportedOperationException("Connect not yet implemented. Implement connect() and remove this exception!"); // 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 - } + + //FRED: --> WAVE4 PART + // Added socket connection + Socket socket = new Socket(this.host, this.port); + + // Create UTF-8 input reader + InputStreamReader inputStreamReader = + new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8); + this.serverReader = new BufferedReader(inputStreamReader); + + // Create UTF-8 output writer + OutputStreamWriter outputStreamWriter = + new OutputStreamWriter(socket.getOutputStream(), StandardCharsets.UTF_8); + this.serverWriter = new BufferedWriter(outputStreamWriter); +} + /** * Authenticate with the server using the simple protocol. From 95fe89a15b1a24b64479a209a65f568a80289c3b Mon Sep 17 00:00:00 2001 From: Fkarau <30944677+Karau1218@users.noreply.github.com> Date: Sun, 7 Dec 2025 11:48:37 -0800 Subject: [PATCH 05/15] working on wave 5 now --- src/ChatterboxClient.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ChatterboxClient.java b/src/ChatterboxClient.java index 7a95f97..1b84098 100644 --- a/src/ChatterboxClient.java +++ b/src/ChatterboxClient.java @@ -225,6 +225,7 @@ public void connect() throws IOException { } + /** * Authenticate with the server using the simple protocol. * From 7d8cb2dffa7b2bb2a49df4a64ad34d7de0f1623a Mon Sep 17 00:00:00 2001 From: Fkarau <30944677+Karau1218@users.noreply.github.com> Date: Mon, 8 Dec 2025 01:13:17 -0800 Subject: [PATCH 06/15] done w wave 5, Working on wave 6 --- src/ChatterboxClient.java | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/ChatterboxClient.java b/src/ChatterboxClient.java index 1b84098..943b327 100644 --- a/src/ChatterboxClient.java +++ b/src/ChatterboxClient.java @@ -247,9 +247,39 @@ public void connect() throws IOException { * @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!"); + // 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!) + + // WAVE 5 + // Read users prompt if there is + String prompt = serverReader.readLine(); + if (prompt != null) { + userOutput.write((prompt + "\n").getBytes(StandardCharsets.UTF_8)); + userOutput.flush(); + } + + // Send username and password + serverWriter.write(username + " " + password + "\n"); + serverWriter.flush(); + + // Read server response + String response = serverReader.readLine(); + if (response == null) { + throw new IOException("Server closed connection unexpectedly."); + } + + if (response.startsWith("Welcome")) { + // Successful login, print welcome message + userOutput.write((response + "\n").getBytes(StandardCharsets.UTF_8)); + userOutput.flush(); + } else { + // Failed authentication + throw new IllegalArgumentException(response); + } + + + } /** From fdb742764894f9332ab28d575cd545e6fd6f5c8c Mon Sep 17 00:00:00 2001 From: Fkarau <30944677+Karau1218@users.noreply.github.com> Date: Mon, 8 Dec 2025 01:34:36 -0800 Subject: [PATCH 07/15] done w wave 6, working on wave 7 --- src/ChatterboxClient.java | 50 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/src/ChatterboxClient.java b/src/ChatterboxClient.java index 943b327..e0fcc30 100644 --- a/src/ChatterboxClient.java +++ b/src/ChatterboxClient.java @@ -295,7 +295,41 @@ 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!"); + // throw new UnsupportedOperationException("Chat streaming not yet implemented. Implement streamChat() and remove this exception!"); + + // FRED: --> + + //ADDED THE PRINT CHATS FOR WAVE 6 + // printIncomingChats(); + + // updated the printincoming chats and added the try method + Thread incomingThread = new Thread(() -> { + try { + printIncomingChats(); + } catch (IOException e) { + // When server disconnects, this will end + } + }); + + Thread outgoingThread = new Thread(() -> { + try { + sendOutgoingChats(); + } catch (IOException e) { + // Writing failed (server likely disconnected) + } + }); + + incomingThread.start(); + outgoingThread.start(); + + try { + incomingThread.join(); + outgoingThread.join(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + + } /** @@ -312,9 +346,16 @@ public void streamChat() throws IOException { * - If an IOException happens, treat it as disconnect: * print a message to userOutput and exit. */ - public void printIncomingChats() { + public void printIncomingChats() throws IOException{ // Listen on serverReader // Write to userOutput, NOT System.out + + // WAVE 6: + String line; + while ((line = serverReader.readLine()) != null) { + userOutput.write((line + "\n").getBytes(StandardCharsets.UTF_8)); + userOutput.flush(); + } } /** @@ -329,10 +370,13 @@ public void printIncomingChats() { * - If writing fails (IOException), the connection is gone: * print a message to userOutput and exit. */ - public void sendOutgoingChats() { + public void sendOutgoingChats() throws IOException{ // Use the userInput to read, NOT System.in directly // loop forever reading user input // write to serverOutput + + // WAVE 7: + } public String getHost() { From 3e82d30e3bc672c32d2fecb626171c34ff6c0d8a Mon Sep 17 00:00:00 2001 From: Fkarau <30944677+Karau1218@users.noreply.github.com> Date: Mon, 8 Dec 2025 01:44:13 -0800 Subject: [PATCH 08/15] added wave 7 --- src/ChatterboxClient.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/ChatterboxClient.java b/src/ChatterboxClient.java index e0fcc30..20c5c2f 100644 --- a/src/ChatterboxClient.java +++ b/src/ChatterboxClient.java @@ -376,6 +376,16 @@ public void sendOutgoingChats() throws IOException{ // write to serverOutput // WAVE 7: + while (true) { + if (!userInput.hasNextLine()) { + break; + } + String message = userInput.nextLine(); + serverWriter.write(message + "\n"); + serverWriter.flush(); + } + + } From 1135a386440de973808812c8e6f6489742a0cd78 Mon Sep 17 00:00:00 2001 From: Fkarau <30944677+Karau1218@users.noreply.github.com> Date: Mon, 8 Dec 2025 02:01:21 -0800 Subject: [PATCH 09/15] modified wave 5 again --- src/ChatterboxClient.java | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/ChatterboxClient.java b/src/ChatterboxClient.java index 20c5c2f..99a860e 100644 --- a/src/ChatterboxClient.java +++ b/src/ChatterboxClient.java @@ -254,25 +254,31 @@ public void authenticate() throws IOException, IllegalArgumentException { // WAVE 5 // Read users prompt if there is String prompt = serverReader.readLine(); - if (prompt != null) { - userOutput.write((prompt + "\n").getBytes(StandardCharsets.UTF_8)); - userOutput.flush(); + if (prompt == null) { + throw new IOException("Server disconnected before authentication prompt."); } + // Prompt sent to the user + userOutput.write((prompt + "\n").getBytes(StandardCharsets.UTF_8)); + userOutput.flush(); + // Send username and password - serverWriter.write(username + " " + password + "\n"); + String loginLine = username + " " + password; + serverWriter.write(loginLine + "\n"); serverWriter.flush(); // Read server response String response = serverReader.readLine(); if (response == null) { - throw new IOException("Server closed connection unexpectedly."); + throw new IOException("Server disconnected during authentication."); } + + //response to the user + userOutput.write((response + "\n").getBytes(StandardCharsets.UTF_8)); + userOutput.flush(); - if (response.startsWith("Welcome")) { - // Successful login, print welcome message - userOutput.write((response + "\n").getBytes(StandardCharsets.UTF_8)); - userOutput.flush(); + // if successful + if (response.startsWith("Welcome")) { } else { // Failed authentication throw new IllegalArgumentException(response); From 9130410a4a61a50c75c6526385ff67a6d39f98e3 Mon Sep 17 00:00:00 2001 From: Fkarau <30944677+Karau1218@users.noreply.github.com> Date: Mon, 8 Dec 2025 02:02:22 -0800 Subject: [PATCH 10/15] modified password txt --- my_passwords.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/my_passwords.txt b/my_passwords.txt index 0332ee1..d6a6f24 100644 --- a/my_passwords.txt +++ b/my_passwords.txt @@ -1,2 +1,3 @@ You can add your passwords here and they won't be committed to your git repository. +Passion1218! \ No newline at end of file From 827c4b0c981cdad9e62ea2129d6f538b0d86da5b Mon Sep 17 00:00:00 2001 From: Fkarau <30944677+Karau1218@users.noreply.github.com> Date: Mon, 8 Dec 2025 17:51:33 -0800 Subject: [PATCH 11/15] labeling each wave per instruction --- src/ChatterboxClient.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/ChatterboxClient.java b/src/ChatterboxClient.java index 99a860e..298686a 100644 --- a/src/ChatterboxClient.java +++ b/src/ChatterboxClient.java @@ -11,7 +11,7 @@ import java.io.OutputStreamWriter; -// WORKING ON FILE NOW: REMOVE LATER -- REMEMBER TO REMOVE THIS LINE: + /** @@ -40,12 +40,12 @@ public class ChatterboxClient { private String password; // Streams for user I/O - private Scanner userInput; - private OutputStream userOutput; + private Scanner userInput; // for reading user input + private OutputStream userOutput; // for printing to user // Readers/Writers for server I/O (set up in connect()) - private BufferedReader serverReader; - private BufferedWriter serverWriter; + private BufferedReader serverReader; // for reading from the server + private BufferedWriter serverWriter; // for writing to the server /** * Program entry. @@ -132,6 +132,9 @@ public static void main(String[] args) { * @return a fully populated ChatterboxOptions * @throws IllegalArgumentException on any bad/missing input */ + + + // This is Wave 1 --> for the command arg lines 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 @@ -149,7 +152,7 @@ public static ChatterboxOptions parseArgs(String[] args) throws IllegalArgumentE try { port = Integer.parseInt(portString); } catch (NumberFormatException e) { - throw new IllegalArgumentException("Port must be a valid integer."); + throw new IllegalArgumentException("The port has to be a valid integer."); } if (port < 1 || port > 65535) { @@ -159,7 +162,7 @@ public static ChatterboxOptions parseArgs(String[] args) throws IllegalArgumentE return new ChatterboxOptions(host, port, username, password); } - +// this is WAVE 2 --> Adding constructors /** * Construct a ChatterboxClient from already-parsed options and user streams. From 7e563ff24815eefc82ae1eb1cdc0934f2ec5a5f3 Mon Sep 17 00:00:00 2001 From: Fkarau <30944677+Karau1218@users.noreply.github.com> Date: Mon, 8 Dec 2025 17:59:39 -0800 Subject: [PATCH 12/15] adding more comments --- src/ChatterboxClient.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/ChatterboxClient.java b/src/ChatterboxClient.java index 298686a..882401f 100644 --- a/src/ChatterboxClient.java +++ b/src/ChatterboxClient.java @@ -180,7 +180,7 @@ public ChatterboxClient(ChatterboxOptions options, InputStream userInput, Output this.userInput = new Scanner(userInput, StandardCharsets.UTF_8); this.userOutput = userOutput; - // NOTE: add // on line 177 --> so it removes the red scuiggle line in line 181 + // NOTE: add // on line 184 --> so it removes the red scuiggle line in line 188 // throw new UnsupportedOperationException("Constructor not yet implemented. Implement ChatterboxClient constructor and remove this exception"); // TODO: copy options.getHost(), getPort(), getUsername(), getPassword() into fields @@ -206,6 +206,9 @@ public ChatterboxClient(ChatterboxOptions options, InputStream userInput, Output * * @throws IOException if the socket cannot be opened */ + + //THis is wave 4 --> Connect + public void connect() throws IOException { // throw new UnsupportedOperationException("Connect not yet implemented. Implement connect() and remove this exception!"); @@ -249,12 +252,14 @@ public void connect() throws IOException { * @throws IOException for network errors * @throws IllegalArgumentException for bad credentials / server rejection */ + + //THis is wave 5 --> Authenticate 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!) - // WAVE 5 + // Read users prompt if there is String prompt = serverReader.readLine(); if (prompt == null) { @@ -303,10 +308,11 @@ public void authenticate() throws IOException, IllegalArgumentException { * * @throws IOException */ + + // this is for wave 6 & 7 --> Chat streaming public void streamChat() throws IOException { // throw new UnsupportedOperationException("Chat streaming not yet implemented. Implement streamChat() and remove this exception!"); - // FRED: --> //ADDED THE PRINT CHATS FOR WAVE 6 // printIncomingChats(); @@ -359,7 +365,7 @@ public void printIncomingChats() throws IOException{ // Listen on serverReader // Write to userOutput, NOT System.out - // WAVE 6: + // WAVE 6 continuation: String line; while ((line = serverReader.readLine()) != null) { userOutput.write((line + "\n").getBytes(StandardCharsets.UTF_8)); From 76df9fabdd9589fb53452771ee37ab41f33f8223 Mon Sep 17 00:00:00 2001 From: Fkarau <30944677+Karau1218@users.noreply.github.com> Date: Mon, 8 Dec 2025 20:17:17 -0800 Subject: [PATCH 13/15] Added more NOTE Commenrs worked on wave 1 - 7. --- src/ChatterboxClient.java | 70 ++++++++++++++++++++++++++++++--------- 1 file changed, 55 insertions(+), 15 deletions(-) diff --git a/src/ChatterboxClient.java b/src/ChatterboxClient.java index 882401f..85f2705 100644 --- a/src/ChatterboxClient.java +++ b/src/ChatterboxClient.java @@ -11,7 +11,10 @@ import java.io.OutputStreamWriter; - +/*Worked on WAVE 1 - 7 +MOST OF MY COMMENTS BEGIN WITH NOTE, some dont have but the main ones +have NOTE + */ /** @@ -44,8 +47,8 @@ public class ChatterboxClient { private OutputStream userOutput; // for printing to user // Readers/Writers for server I/O (set up in connect()) - private BufferedReader serverReader; // for reading from the server - private BufferedWriter serverWriter; // for writing to the server + private BufferedReader serverReader; // for reading from the network socket + private BufferedWriter serverWriter; // for writing to the network socket /** * Program entry. @@ -139,10 +142,14 @@ public static ChatterboxOptions parseArgs(String[] args) throws IllegalArgumentE // 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"); + + // NOTE: We need 4 arguments of validation for the program if ( args.length != 4) { throw new IllegalArgumentException("Expected 4 arguments: HOST PORT USERNAME PASSWORD"); } + + //NOTE: The expeced order based on LINE 121 String host = args[0]; String portString = args[1]; String username = args[2]; @@ -150,8 +157,11 @@ public static ChatterboxOptions parseArgs(String[] args) throws IllegalArgumentE int port; try { + // NOTE: This attemps to convert port String to Intger + //Its required coz the PORT is numerical port = Integer.parseInt(portString); } catch (NumberFormatException e) { + // NOTE: It rhrows when the string isnt a valid numnber throw new IllegalArgumentException("The port has to be a valid integer."); } @@ -177,14 +187,18 @@ public static ChatterboxOptions parseArgs(String[] args) throws IllegalArgumentE * @param userOutput stream to print data to the user */ public ChatterboxClient(ChatterboxOptions options, InputStream userInput, OutputStream userOutput) { + // NOTE: THE utf 8 is for the character encoding + // NOTE: the userinput gets the input then the chat starts this.userInput = new Scanner(userInput, StandardCharsets.UTF_8); + this.userOutput = userOutput; // NOTE: add // on line 184 --> so it removes the red scuiggle line in line 188 // throw new UnsupportedOperationException("Constructor not yet implemented. Implement ChatterboxClient constructor and remove this exception"); // TODO: copy options.getHost(), getPort(), getUsername(), getPassword() into fields - + // NOTE: In the this.host (line 197 - 201) the constructor just saves the data + // into the client's memory for them to be used with other methods this.host = options.getHost(); this.port = options.getPort(); this.username = options.getUsername(); @@ -216,10 +230,17 @@ public void connect() throws IOException { // hint: get the streams from the sockets, use those to create the InputStreamReader/OutputStreamWriter and the BufferedReader/BufferedWriter //FRED: --> WAVE4 PART - // Added socket connection + // NOTE: Added socket connection + // NOTE: The socket line makes the call --> then establish connection + // if server's not there rhe exception is throen + // NOTE: The socket initializes the connection to the specified host and port + Socket socket = new Socket(this.host, this.port); - // Create UTF-8 input reader + // NOTE: Create UTF-8 input reader + // NOTE: the socketgetinputstream --> gets the byte stram from the netword + // NOTE: the inputstramreader changes the raw bytes to readable UTF 8 Texts + // that we can read InputStreamReader inputStreamReader = new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8); this.serverReader = new BufferedReader(inputStreamReader); @@ -227,7 +248,9 @@ public void connect() throws IOException { // Create UTF-8 output writer OutputStreamWriter outputStreamWriter = new OutputStreamWriter(socket.getOutputStream(), StandardCharsets.UTF_8); - this.serverWriter = new BufferedWriter(outputStreamWriter); + // NOTE: The buffered writer helps send the text to the server --> + // kinda like an outbox in gmail + this.serverWriter = new BufferedWriter(outputStreamWriter); } @@ -260,35 +283,40 @@ public void authenticate() throws IOException, IllegalArgumentException { // send messages using serverWriter (don't forget to flush!) - // Read users prompt if there is + // NOTE: Read users prompt if there is String prompt = serverReader.readLine(); if (prompt == null) { throw new IOException("Server disconnected before authentication prompt."); } - // Prompt sent to the user + // NOTE: Prompt sent to the user + // NOTE: The getBytes() --> changes tect into bytes to be sent + // to the outstream userOutput.write((prompt + "\n").getBytes(StandardCharsets.UTF_8)); userOutput.flush(); - // Send username and password + // NOTE: Send username and password as required by the server String loginLine = username + " " + password; serverWriter.write(loginLine + "\n"); + // NOTE: The flush() sends it to the server directly for the login attempt serverWriter.flush(); // Read server response String response = serverReader.readLine(); + // NOTE: If response returns null, server disconnects if (response == null) { throw new IOException("Server disconnected during authentication."); } - //response to the user + //NOTE: server's response to the user userOutput.write((response + "\n").getBytes(StandardCharsets.UTF_8)); userOutput.flush(); - // if successful + //NOTE: if successful, you get welcome, if not an exception is thrown if (response.startsWith("Welcome")) { } else { - // Failed authentication + // NOTE: Failed authentication throws an exception + // if the main method catches the exception it directly exits the client throw new IllegalArgumentException(response); } @@ -313,6 +341,10 @@ public void authenticate() throws IOException, IllegalArgumentException { public void streamChat() throws IOException { // throw new UnsupportedOperationException("Chat streaming not yet implemented. Implement streamChat() and remove this exception!"); + // NOTE: in this wave, the incoming thread runs the thread printincomingchats everytime + // the client can then keep reading the messages of the server + // The lambda makes the thread shorter and cleaner. (eg line 351 with ->) + //ADDED THE PRINT CHATS FOR WAVE 6 // printIncomingChats(); @@ -322,7 +354,8 @@ public void streamChat() throws IOException { try { printIncomingChats(); } catch (IOException e) { - // When server disconnects, this will end + // When server disconnects, this will throw the exception and + // the thread ends there } }); @@ -334,10 +367,12 @@ public void streamChat() throws IOException { } }); + // NOTE: Both of these start at the same time incomingThread.start(); outgoingThread.start(); try { + // this tells the main thread to wait until its done reading messages incomingThread.join(); outgoingThread.join(); } catch (InterruptedException e) { @@ -367,8 +402,12 @@ public void printIncomingChats() throws IOException{ // WAVE 6 continuation: String line; + // NOTE: As long as the readline is not null, the loop continues + // as long as there are readlines or texts added. It will continue reading while ((line = serverReader.readLine()) != null) { + // userOutput.write((line + "\n").getBytes(StandardCharsets.UTF_8)); + // NOTE: Flush so the user sees the message ASAP userOutput.flush(); } } @@ -392,8 +431,9 @@ public void sendOutgoingChats() throws IOException{ // WAVE 7: while (true) { + // NOTE: This is to check if there is anothe rline to read if (!userInput.hasNextLine()) { - break; + break; // if not found break and exit } String message = userInput.nextLine(); serverWriter.write(message + "\n"); From d6b19e2a3db33a0fd9753e447eba04d22aa22323 Mon Sep 17 00:00:00 2001 From: Fkarau <30944677+Karau1218@users.noreply.github.com> Date: Mon, 8 Dec 2025 23:07:02 -0800 Subject: [PATCH 14/15] changed some 'NOTE' In the comments due to addtional code --- src/ChatterboxClient.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/ChatterboxClient.java b/src/ChatterboxClient.java index 85f2705..49af291 100644 --- a/src/ChatterboxClient.java +++ b/src/ChatterboxClient.java @@ -10,8 +10,8 @@ import java.io.InputStreamReader; import java.io.OutputStreamWriter; - -/*Worked on WAVE 1 - 7 +/* +Worked on WAVE 1 - 7 MOST OF MY COMMENTS BEGIN WITH NOTE, some dont have but the main ones have NOTE */ @@ -149,7 +149,7 @@ public static ChatterboxOptions parseArgs(String[] args) throws IllegalArgumentE } - //NOTE: The expeced order based on LINE 121 + //NOTE: The expeced order based on LINE 124 String host = args[0]; String portString = args[1]; String username = args[2]; @@ -157,7 +157,7 @@ public static ChatterboxOptions parseArgs(String[] args) throws IllegalArgumentE int port; try { - // NOTE: This attemps to convert port String to Intger + // NOTE: This is for converting the portString to Integer //Its required coz the PORT is numerical port = Integer.parseInt(portString); } catch (NumberFormatException e) { @@ -193,11 +193,11 @@ public ChatterboxClient(ChatterboxOptions options, InputStream userInput, Output this.userOutput = userOutput; - // NOTE: add // on line 184 --> so it removes the red scuiggle line in line 188 + // NOTE: add // on line 196-> so it removes the red scuiggle line in line 188 // throw new UnsupportedOperationException("Constructor not yet implemented. Implement ChatterboxClient constructor and remove this exception"); // TODO: copy options.getHost(), getPort(), getUsername(), getPassword() into fields - // NOTE: In the this.host (line 197 - 201) the constructor just saves the data + // NOTE: In the this.host (line 202 - 205) the constructor just saves the data // into the client's memory for them to be used with other methods this.host = options.getHost(); this.port = options.getPort(); @@ -303,7 +303,7 @@ public void authenticate() throws IOException, IllegalArgumentException { // Read server response String response = serverReader.readLine(); - // NOTE: If response returns null, server disconnects + // NOTE: If response is null, server disconnects if (response == null) { throw new IOException("Server disconnected during authentication."); } @@ -342,7 +342,7 @@ public void streamChat() throws IOException { // throw new UnsupportedOperationException("Chat streaming not yet implemented. Implement streamChat() and remove this exception!"); // NOTE: in this wave, the incoming thread runs the thread printincomingchats everytime - // the client can then keep reading the messages of the server + //thus helps the client can then keep reading the messages of the server // The lambda makes the thread shorter and cleaner. (eg line 351 with ->) From 487100ec8cc3c384076b8a78ade2be9ff6238487 Mon Sep 17 00:00:00 2001 From: Fkarau <30944677+Karau1218@users.noreply.github.com> Date: Mon, 8 Dec 2025 23:17:10 -0800 Subject: [PATCH 15/15] changed a comment --- src/ChatterboxClient.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ChatterboxClient.java b/src/ChatterboxClient.java index 49af291..2fbd602 100644 --- a/src/ChatterboxClient.java +++ b/src/ChatterboxClient.java @@ -149,7 +149,7 @@ public static ChatterboxOptions parseArgs(String[] args) throws IllegalArgumentE } - //NOTE: The expeced order based on LINE 124 + //NOTE: The expeced order based on the required argument order String host = args[0]; String portString = args[1]; String username = args[2];