Skip to content

Commit

Permalink
Update big message code.
Browse files Browse the repository at this point in the history
  • Loading branch information
pvcraven committed Mar 15, 2019
1 parent dfff70c commit bf711ce
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 18 deletions.
31 changes: 27 additions & 4 deletions tcp_receive_big_message.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -31,6 +48,9 @@

done = False
chunks = 0
connection = None
client_ip = None
client_port = None

while not done:

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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))
print(f"Done receiving message.")
print(f"Processed {len(full_message)} bytes in {chunks} chunks.")
33 changes: 19 additions & 14 deletions tcp_send_big_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

0 comments on commit bf711ce

Please sign in to comment.