From 5cc91f3b67037319aed970a359f4577e219c8653 Mon Sep 17 00:00:00 2001 From: Paul Vincent Craven Date: Wed, 24 Mar 2021 11:14:46 -0500 Subject: [PATCH] Work on fixing up the Java send/receive message code --- ReceiveMultipleMessages.java | 22 ++++++++++++---------- ReceiveOneShot.java | 31 ++++++++++++++++++------------- 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/ReceiveMultipleMessages.java b/ReceiveMultipleMessages.java index 0a7a883..f228b6b 100644 --- a/ReceiveMultipleMessages.java +++ b/ReceiveMultipleMessages.java @@ -78,26 +78,28 @@ public static void handleConnection() throws Exception { } else if (myKey.isReadable()) { SocketChannel myClient = (SocketChannel) myKey.channel(); - ByteBuffer myBuffer = ByteBuffer.allocate(2048); - myClient.read(myBuffer); - String result = new String(myBuffer.array()).trim(); - fullMessage = fullMessage + result; - - // System.out.println("Message received: " + result); - chunks += 1; + ByteBuffer myBuffer = ByteBuffer.allocate(65536); + int bytesRead = myClient.read(myBuffer); + String result = new String(myBuffer.array()); + result = result.substring(0, bytesRead); + if (result.length() > 0) { + fullMessage = fullMessage + result; + chunks += 1; + System.out.println("Message received " + result.length() + " bytes: \"" + result + "\""); + } if (result.endsWith("\n")) { myClient.close(); - myServerSocketChannel.close(); - channelSelector.close(); done = true; - System.out.println("It's time to close connection as we got a Z"); + System.out.println("It's time to close connection as we got a \\n"); endTime = System.currentTimeMillis(); } } keyIterator.remove(); } } + myServerSocketChannel.close(); + channelSelector.close(); long totalTime = endTime - startTime; System.out.println("Done receiving " + fullMessage.length() + " in " + chunks + " chunks over " + totalTime + " ms."); } diff --git a/ReceiveOneShot.java b/ReceiveOneShot.java index 4da4bf9..ac181c5 100644 --- a/ReceiveOneShot.java +++ b/ReceiveOneShot.java @@ -40,8 +40,9 @@ public void setupSocket() throws Exception { System.out.println("\nStarting..."); } - public void checkConnection() throws Exception { - + public boolean checkConnection() throws Exception { + // Set to true when we have our whole message + boolean done = false; // Selects a set of keys whose corresponding channels are ready for I/O operations // For blocking I/O use a select with no data: @@ -72,29 +73,33 @@ public void checkConnection() throws Exception { // Tests whether this key's channel is ready for reading } else if (myKey.isReadable()) { - SocketChannel myClient = (SocketChannel) myKey.channel(); - ByteBuffer myBuffer = ByteBuffer.allocate(2048); - myClient.read(myBuffer); - String result = new String(myBuffer.array()).trim(); - - System.out.println("Message received: " + result); + SocketChannel myClient = (SocketChannel) myKey.channel(); + ByteBuffer myBuffer = ByteBuffer.allocate(65536); + int bytesRead = myClient.read(myBuffer); + String result = new String(myBuffer.array()); + result = result.substring(0, bytesRead); + if (result.length() > 0) { + System.out.println("Message received " + result.length() + " bytes: \"" + result + "\""); + } - if (result.endsWith("Z")) { + if (result.endsWith("\n")) { myClient.close(); - myServerSocketChannel.close(); -// channelSelector.close(); + done = true; System.out.println("It's time to close connection as we got a Z"); } } keyIterator.remove(); } + return done; } public static void main(String[] args) throws Exception { ReceiveOneShot receiveOneShot = new ReceiveOneShot(); receiveOneShot.setupSocket(); - while(true) - receiveOneShot.checkConnection(); + + boolean done = false; + while(!done) + done = receiveOneShot.checkConnection(); } } \ No newline at end of file