From bf711ceeb51d16e3911ccaf68a39ba4baccd77e2 Mon Sep 17 00:00:00 2001 From: Paul Vincent Craven Date: Thu, 14 Mar 2019 19:24:15 -0500 Subject: [PATCH] Update big message code. --- tcp_receive_big_message.py | 31 +++++++++++++++++++++++++++---- tcp_send_big_message.py | 33 +++++++++++++++++++-------------- 2 files changed, 46 insertions(+), 18 deletions(-) diff --git a/tcp_receive_big_message.py b/tcp_receive_big_message.py index cc14185..ce90e32 100644 --- a/tcp_receive_big_message.py +++ b/tcp_receive_big_message.py @@ -1,11 +1,26 @@ import socket +import time +# This is your buffer size. +# You can't receive anything more than the buffer size at one time. BUFFER_SIZE = 65535 +# This should be the IP address of the computer you run this +# code on (the server). It should be the SAME IP address that +# the client hooks up to. +# Note: If you use '127.0.0.1' you can only receive connections +# from the same computer. Outside computers cannot connect to a +# computer listening to 127.0.0.1. my_ip_address = '127.0.0.1' my_ip_port = 10000 -# Our full message +# We will loop until we get a connection or we get data. We don't want +# to check thousands of times per second for these because that would +# max our CPU. If we have nothing to do, how long we wait before we +# check again. +DELAY = 0.1 + +# Our full message. Starts empty. full_message = b"" # We need to build a "state machine" that keeps @@ -22,7 +37,9 @@ my_socket.settimeout(0.0) # Tell the socket it will be listening on my_ip_address, and my_port. -my_socket.bind((my_ip_address, my_ip_port)) +# Note that Python expects ip and port as a list +listen_to = (my_ip_address, my_ip_port) +my_socket.bind(listen_to) # We are going to be listening as a server, not connecting as a client. # The "1" specifies the size of the backlog of connections we allow before @@ -31,6 +48,9 @@ done = False chunks = 0 +connection = None +client_ip = None +client_port = None while not done: @@ -40,6 +60,8 @@ # Get a connection, and the address that hooked up to us. # The 'client address' is an array that has the IP and the port. connection, client_address = my_socket.accept() + client_ip = client_address[0] + client_port = client_address[1] state = CONNECTED except BlockingIOError: pass @@ -52,7 +74,7 @@ chunks += 1 if len(data) > 0: - print("Data from {}:{} \"{}\"".format(client_address[0], client_address[1], data)) + print(f"Data from {client_ip}:{client_port} '{data}'") # Append this chunk to the full message full_message += data @@ -70,4 +92,5 @@ # Close the socket. No socket operations can happen after this. my_socket.close() -print("Done receiving message. Processed {} bytes in {} chunks.".format(len(full_message), chunks)) \ No newline at end of file +print(f"Done receiving message.") +print(f"Processed {len(full_message)} bytes in {chunks} chunks.") \ No newline at end of file diff --git a/tcp_send_big_message.py b/tcp_send_big_message.py index 4064731..082f106 100644 --- a/tcp_send_big_message.py +++ b/tcp_send_big_message.py @@ -6,27 +6,32 @@ # Size of our message. # Must be at least 1 -message_size_in_bytes = 300000 +message_size_in_bytes = 600000 -# Message as a byte array. (Hence the b at the front.) -# Send a bunch of x's, and an "END" +# Create a message as a byte array. +# Use multiplication to quickly create an array of n-1 bytes. +# Add a \n at the end which we will use to detect end-of-message. my_message = b"X" * (message_size_in_bytes - 1) + b"\n" try: - # Open a socket - my_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + # Create a socket for IPv4 (AF_INET), TCP stream (SOCK_STREAM) + my_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - # Connect to this server, and this port. - my_socket.connect((server_ip_address, server_ip_port)) + # Connect to this specified server and port + # Note that Python expects ip and port as a list + destination = (server_ip_address, server_ip_port) + my_socket.connect(destination) - # Send the message - my_socket.sendall(my_message) + # Send the message + my_socket.sendall(my_message) - # Close the socket - my_socket.close() + # Close the socket + my_socket.close() -except: - # Something bad happened. - print("Unable to send the message.") +except ConnectionRefusedError: + print("Client refused to accept the connection.") + +except Exception as e: + print("Error: ", e) print("Done") \ No newline at end of file