select.select and socket.setblocking

Jean-Paul Calderone exarkun at divmod.com
Wed Dec 31 10:31:00 EST 2008


On Wed, 31 Dec 2008 15:48:50 +0100, Francesco Bochicchio <bockman at virgilio.it> wrote:
>< ... >
>>>Uhm. In my experience, with TCP protocol recv only returned less than
>>>the required bytes if the remote end disconnects. I always check the
>>
>>What if the sending end actually sent less than you asked for ?
>>
>>-srp
>
>In blocking mode and with TCP protocol, the recv waits until more bytes are 
>received -  mixing up the next message with the previous one and then 
>loosing the 'sync' and being unable to interpretate the received data -  or 
>the remote end disconnects.

Nope, this isn't how TCP works, whether your sockets are blocking or non-
blocking.  It's easy to see this:

  >>> s = socket.socket()
  >>> s.connect(('www.google.com', 80))
  >>> s.sendall('GET / HTTP/1.1\r\nHost: www.google.com\r\nConnection: keep-alive\r\n\r\n')
  >>> len(s.recv(1024 * 1024))
  6191
  >>> s.sendall('GET / HTTP/1.1\r\nHost: www.google.com\r\nConnection: keep-alive\r\n\r\n')
  >>> len(s.recv(1024 * 1024))
  6191
  >>> s.sendall('GET / HTTP/1.1\r\nHost: www.google.com\r\nConnection: keep-alive\r\n\r\n')
  >>> len(s.recv(1024 * 1024))
  6191
  >>> s.sendall('GET / HTTP/1.1\r\nHost: www.google.com\r\nConnection: keep-alive\r\n\r\n')
  >>> len(s.recv(1024 * 1024))
  6191
  >>>

Numerous blocking recv calls which returned fewer bytes than asked for
without the connection having been closed.

The only guarantee you get with a blocking socket is that the recv will
return at least one byte and at most the number of bytes you asked for
(or raise an exception instead).

On a LAN, it's likely that you'll generally get the exact number of bytes
which the sender passed to one call of send (until the sender starts to
pass really huge strings to send, then it'll get split up) just because
the network has lots of capacity compared to the traffic you're putting
on it.  However, even on a LAN it's not guaranteed, and on the internet,
it's extremely likely that this won't happen most of the time.

Jean-Paul



More information about the Python-list mailing list