From 122fa62d5c5cd9971e3c1a8092fe6a84fc6580c8 Mon Sep 17 00:00:00 2001 From: Paul Vincent Craven Date: Wed, 24 Mar 2021 10:16:09 -0500 Subject: [PATCH] Update for Java multiple message support. --- ReceiveMultipleMessages.java | 8 +++-- SendMultipleMessages.java | 55 +++++++++++++++++++++++++++++++++++ tcp_send_multiple_messages.py | 7 +++-- 3 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 SendMultipleMessages.java diff --git a/ReceiveMultipleMessages.java b/ReceiveMultipleMessages.java index b9ce53f..0a7a883 100644 --- a/ReceiveMultipleMessages.java +++ b/ReceiveMultipleMessages.java @@ -15,12 +15,16 @@ public class ReceiveMultipleMessages { public static void handleConnection() throws Exception { + // Modify this to match the computer you are connecting to + String ipAddress = "127.0.0.1"; + int port = 10000; + // Selector: multiplexor of SelectableChannel objects Selector channelSelector = Selector.open(); // selector is open here // ServerSocketChannel: selectable channel for stream-oriented listening sockets ServerSocketChannel myServerSocketChannel = ServerSocketChannel.open(); - InetSocketAddress myAddress = new InetSocketAddress("127.0.0.1", 10000); + InetSocketAddress myAddress = new InetSocketAddress(ipAddress, port); // Binds the channel's socket to a local address and configures the socket to listen for connections myServerSocketChannel.bind(myAddress); @@ -82,7 +86,7 @@ public static void handleConnection() throws Exception { // System.out.println("Message received: " + result); chunks += 1; - if (result.endsWith("Z")) { + if (result.endsWith("\n")) { myClient.close(); myServerSocketChannel.close(); channelSelector.close(); diff --git a/SendMultipleMessages.java b/SendMultipleMessages.java new file mode 100644 index 0000000..33f50f9 --- /dev/null +++ b/SendMultipleMessages.java @@ -0,0 +1,55 @@ +import java.io.PrintWriter; +import java.io.IOException; +import java.net.InetAddress; +import java.net.Socket; + +public class SendMultipleMessages { + + private static void sendData(int totalBytes, int messageSizeInBytes) + throws IOException { + + // Modify this to match to be THIS computer's address + String ipAddress = "127.0.0.1"; + int serverPort = 10000; + + InetAddress serverAddress = InetAddress.getByName(ipAddress); + int messagesToSend = totalBytes / messageSizeInBytes; + + String myData = ""; + for(int i = 0; i < messageSizeInBytes; i++) { + myData += "X"; + } + + Socket socket = new Socket(serverAddress, serverPort); + + // Try changing this to false and see if it changes how packets are sent + boolean autoFlush = true; + PrintWriter out = new PrintWriter(socket.getOutputStream(), autoFlush); + + for (int i = 0; i < messagesToSend; i++) { + out.print(myData); + // Try enabling this and see if it changes how packets are sent + // out.flush(); + } + out.print("\n"); + + out.flush(); + + socket.close(); + } + public static void main(String[] args) throws Exception { + + // How many bytes to send + int totalBytes = 5000; + + // How big each message will be + int messageSizeInBytes = 10; + + System.out.println("Sending " + totalBytes + "bytes in " + messageSizeInBytes + " byte chunks."); + + // Send the data + sendData(totalBytes, messageSizeInBytes); + + System.out.println("Done sending message."); + } +} \ No newline at end of file diff --git a/tcp_send_multiple_messages.py b/tcp_send_multiple_messages.py index 6f0c1e2..fb02913 100644 --- a/tcp_send_multiple_messages.py +++ b/tcp_send_multiple_messages.py @@ -4,6 +4,7 @@ server_ip_address = '127.0.0.1' server_ip_port = 10000 + def send_data(total_bytes, message_size_in_bytes): """ Send a bunch of messages that sum "total_bytes" of data. Break each @@ -15,7 +16,7 @@ def send_data(total_bytes, message_size_in_bytes): # Message as a byte array. (Hence the b at the front.) # Send byte array with an X: b"X" # Repeat this (message_size_in_bytes - 1) times. - my_message = b"X" * (message_size_in_bytes) + my_message = b"X" * (message_size_in_bytes - 1) # Open a socket my_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) @@ -36,6 +37,7 @@ def send_data(total_bytes, message_size_in_bytes): print("Done") + def main(): """ Main program. """ @@ -47,4 +49,5 @@ def main(): print(f"Sending {total_bytes:,} bytes in {message_size_in_bytes} byte chunks.") send_data(total_bytes, message_size_in_bytes) -main() \ No newline at end of file + +main()