Python doesn't detect a closed socket until the second send

前端 未结 2 1578
一个人的身影
一个人的身影 2020-12-31 04:26

When I close the socket on one end of a connection, the other end gets an error the second time it sends data, but not the first time:

import socket

server          


        
相关标签:
2条回答
  • 2020-12-31 05:00

    This is expected, and how the TCP/IP APIs are implemented (so it's similar in pretty much all languages and on all operating systems)

    The short story is, you cannot do anything to guarantee that a send() call returns an error directly if that send() call somehow cannot deliver data to the other end. send/write calls just delivers the data to the TCP stack, and it's up to the TCP stack to deliver it when it can.

    TCP is also just a transport protocol, if you need to know if your application "messages" have reached the other end, you need to implement that yourself(some form of ACK), as part of your application protocol - there's no other free lunch.

    However - if you read() from a socket, you can get notified immediatly when an error occurs, or when the other end closed the socket - you usually need to do this in some form of multiplexing event loop (that is, using select/poll or some other IO multiplexing facility).

    Just note that you cannot read() from a socket to learn whether the most recent send/write succeded, Here's a few cases as of why (but it's the cases one doesn't think about that always get you)

    • several write() calls got buffered up due to network congestion, or because the tcp window was closed (perhaps a slow reader) and then the other end closes the socket or a hard network error occurs, thus you can't tell if if was the last write that didn't get through, or a write you did 30 seconds ago.
    • Network error, or firewall silently drops your packets (no ICMP replys are generated), You will have to wait until TCP times out the connection to get an error which can be many seconds, usually several minutes.
    • TCP is busy doing retransmission as you call send - maybe those retransmissions generate an error.(really the same as the first case)
    0 讨论(0)
  • 2020-12-31 05:01

    As per the docs, try calling sock.shutdown() before the call to sock.close().

    0 讨论(0)
提交回复
热议问题