CPSC 3600 - Buffering Large Data Packets

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall with Kai
GameKnowt Play
New
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/5

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

6 Terms

1
New cards

What does this code do?

tcpClient.send(message.encode())

newMessage = tcpClient.recv(2048)

It tells recv to use a 2048 byte recieve buffer

<p>It tells recv to use a 2048 byte recieve buffer</p>
2
New cards

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

3
New cards

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

<p>It packs the message length with the message and sends it out</p><p>- We send the message length as an unsigned short</p>
4
New cards

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

...

knowt flashcard image
5
New cards

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()

knowt flashcard image
6
New cards

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!

knowt flashcard image