1/5
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
What does this code do?
tcpClient.send(message.encode())
newMessage = tcpClient.recv(2048)
It tells recv to use a 2048 byte recieve buffer
What happens if we need to receive more data than the receive buffer can handle?
Buffer the messages ourselves until all data is received then return to our application
What does this code do?
# Create a message with a header containing the length
message = "I love tigers"*1000message_length = len(message)
data = pack(f"!H{message_length}s", message_length, message.encode()) # "!H13000s"
conn.send(data)
It packs the message length with the message and sends it out
- We send the message length as an unsigned short
Code example : Receiving a message with a length header
Extract the length value from the message
# Begin receiving a new message
data = conn.recv(1024)
fixed_length_header = data[:2] # Array slice: returns the first two elements
length = unpack("!H", fixed_length_header)[0] # unpack returns tuple, we want just # the first element
message = data[2:] # Array slice: return all elements after 2nd
...
Code example : Receiving a mesage with a length header #2
# loop until we've received enough data
while len(message) < length:
length_remaining = length - len(message)
if length_remaining > 1024:
data = conn.recv(1024) # receive more data
message += data # concatenate the data to the rest of the message
else:
data = conn.recv(length_remaining) # receive more data
message += data # concatenate the data to the rest of the message
# once the while loop has exited, we can decode and return the message
return message.decode()
Code example
def get_next_message():
# Protocol: [length of payload, 2 bytes][payload]
# Extract the length value from the message# Begin receiving a new message
data = conn.recv(1024)
length = data[0:2] # Array slice: returns the first two elements
length = unpack("!H", length)[0] # unpack returns tuple, we want first element
payload = data[2:] # Array slice: return all elements after 2nd
# loop until we've received enough data
while len(payload) < length:
length_remaining = length - len(message)
if length_remaining > 1024:
data = conn.recv(1024) # receive more data
message += data # concatenate the data to the rest of the message
else:
data = conn.recv(length_remaining) # receive more data
message += data # concatenate the data to the rest of the message
# once the while loop has exited, we can decode and return the message
return payload.decode()
[17]Clemson Is Great!