Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions Doc/howto/sockets.rst
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,11 @@ request, then reads a reply. That's it. The socket is discarded. This means tha
a client can detect the end of the reply by receiving 0 bytes.

But if you plan to reuse your socket for further transfers, you need to realize
that *there is no* :abbr:`EOT (End of Transfer)` *on a socket.* I repeat: if a socket
``send`` or ``recv`` returns after handling 0 bytes, the connection has been
broken. If the connection has *not* been broken, you may wait on a ``recv``
that *there is no* :abbr:`EOT (End of Transfer)` *on a socket.* If a ``recv``
returns 0 bytes, the connection has been broken. In contrast, you should never
call ``send`` on a broken socket, as it will raise an :exc:`OSError` rather than
returning 0. If the connection has *not* been broken, you may wait on a
``recv``
forever, because the socket will *not* tell you that there's nothing more to
read (for now). Now if you think about that a bit, you'll come to realize a
fundamental truth of sockets: *messages must either be fixed length* (yuck), *or
Expand Down Expand Up @@ -201,6 +203,8 @@ length message::
sent = self.sock.send(msg[totalsent:])
if sent == 0:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually think that we can get rid of this if clause and the RuntimeError exception since send will already raise an error on a broken connection. That would also make more sense because your comment below is basically saying that send raises an error automatically so there is no reason to check its return value.

raise RuntimeError("socket connection broken")
# Note: in practice, send() on a broken connection
# raises OSError rather than returning 0.
totalsent = totalsent + sent

def myreceive(self):
Expand Down
Loading