user3117244
user3117244

Reputation: 143

Need some clarification on how socket.recv behaves

I'm trying to write an IRC bot but I'm not exactly sure how the receiving of data works. What I currently have:

while True:
    data = socket.recv(1024)

    #process data

Let's say that for whatever reason it takes it more time to process the data, what would happen if something is sent at that time? Will it get skipped or get added to some sort of a queue and processed after the current one is done?

Upvotes: 1

Views: 123

Answers (1)

ρss
ρss

Reputation: 5315

Depending upon the protocol type the behavior will be different.

TCP:

The TCP RFC clearly states:

TCP provides a means for the receiver to govern the amount of data sent by the sender. This is achieved by returning a "window" with every ACK indicating a range of acceptable sequence numbers beyond the last segment successfully received. The window indicates an allowed number of octets that the sender may transmit before receiving further permission.

Also from wikipedia the information is similar:

TCP uses an end-to-end flow control protocol to avoid having the sender send data too fast for the TCP receiver to receive and process it reliably. For example, if a PC sends data to a smartphone that is slowly processing received data, the smartphone must regulate the data flow so as not to be overwhelmed. TCP uses a sliding window flow control protocol. In each TCP segment, the receiver specifies in the receive window field the amount of additionally received data (in bytes) that it is willing to buffer for the connection. The sending host can send only up to that amount of data before it must wait for an acknowledgment and window update from the receiving host.

UDP:

UDP doesn't have any flow control mechanism as TCP. However there is an other implementation of UDP such as RUDP that have some of the features of TCP like flow control.

Here is an other interesting link for the differences between TCP & UDP.

Upvotes: 1

Related Questions