当内部缓冲区填满时 TCP 会发生什么
What happens in TCP when the internal buffer fills up
假设我们有以下 TCP 套接字设置,客户端可以在其中向服务器发送任意数据。将以下内容视为伪代码。
def client():
while True:
data = source.get_data()
client_socket.send(data)
服务器读取数据并使用它来做某事...
def server():
while True:
data += socket.recv(4096)
parsed_data = parse_data(data)
cpu_intensive_task(parsed_data)
让我们假设客户端发送数据的速度比服务器处理速度快得多。 内部网络缓冲区可以填满吗?我假设答案是肯定的...
如果是这样,那么 TCP 协议是否指定了在这种情况下会发生什么?这些丢弃的数据包是否被视为在传输过程中丢失并像其他任何丢失的数据包一样重新传输?
或者这些数据包真的丢失了,这是我在 TCP 之上设计自己的通信协议时必须考虑的问题吗?
答案是否因操作系统而异?
作为记录,上面的系统应该有某种拥塞控制机制,服务器可以使用它来告诉客户端它负载很重或者它又空闲了。但是,我很想知道默认情况下 TCP 在这种情况下的行为方式。
Can the internal network buffer fill up?
有两个 内部缓冲区:发送缓冲区和接收缓冲区。两者都可以填满。
I assume the answer is yes...
是的。
If so, then does the TCP protocol specify what will happen in this scenario?
是的。
Are these discarded packets
没有丢弃的数据包。在这种情况下,TCP 不会丢弃数据包。发送缓冲区填满时会发生什么取决于您是处于阻塞模式还是 non-blocking 模式,或者您是否使用异步 API:
- 屏蔽模式:发送者屏蔽
- non-blocking模式:发送方收到错误EAGAIN/EWOULDBLOCK
- 异步:操作继续延迟。
treated as if they were lost in transit and just re-transmitted like any other lost packages?
没有。见上文。
Or are these packets truly lost
没有。见上文。
and this something I have to consider when desiging my own communication protocol on top of the TCP?
没有。看上面。但是,在协议的 实现 中,您必须考虑各种条件。
Does the answer vary between operating systems?
没有
For a record, the system above should have some kind of congestion control mechanism
TCP已经有了拥塞控制机制。
which server could use to tell the client that it's under heavy load
怎么样?如果客户端不读取,服务器怎么能告诉它任何信息?
or it's free again. However, I'm curious to know how the TCP by the default would behave in this scenario.
见上文。
假设我们有以下 TCP 套接字设置,客户端可以在其中向服务器发送任意数据。将以下内容视为伪代码。
def client():
while True:
data = source.get_data()
client_socket.send(data)
服务器读取数据并使用它来做某事...
def server():
while True:
data += socket.recv(4096)
parsed_data = parse_data(data)
cpu_intensive_task(parsed_data)
让我们假设客户端发送数据的速度比服务器处理速度快得多。 内部网络缓冲区可以填满吗?我假设答案是肯定的...
如果是这样,那么 TCP 协议是否指定了在这种情况下会发生什么?这些丢弃的数据包是否被视为在传输过程中丢失并像其他任何丢失的数据包一样重新传输?
或者这些数据包真的丢失了,这是我在 TCP 之上设计自己的通信协议时必须考虑的问题吗?
答案是否因操作系统而异?
作为记录,上面的系统应该有某种拥塞控制机制,服务器可以使用它来告诉客户端它负载很重或者它又空闲了。但是,我很想知道默认情况下 TCP 在这种情况下的行为方式。
Can the internal network buffer fill up?
有两个 内部缓冲区:发送缓冲区和接收缓冲区。两者都可以填满。
I assume the answer is yes...
是的。
If so, then does the TCP protocol specify what will happen in this scenario?
是的。
Are these discarded packets
没有丢弃的数据包。在这种情况下,TCP 不会丢弃数据包。发送缓冲区填满时会发生什么取决于您是处于阻塞模式还是 non-blocking 模式,或者您是否使用异步 API:
- 屏蔽模式:发送者屏蔽
- non-blocking模式:发送方收到错误EAGAIN/EWOULDBLOCK
- 异步:操作继续延迟。
treated as if they were lost in transit and just re-transmitted like any other lost packages?
没有。见上文。
Or are these packets truly lost
没有。见上文。
and this something I have to consider when desiging my own communication protocol on top of the TCP?
没有。看上面。但是,在协议的 实现 中,您必须考虑各种条件。
Does the answer vary between operating systems?
没有
For a record, the system above should have some kind of congestion control mechanism
TCP已经有了拥塞控制机制。
which server could use to tell the client that it's under heavy load
怎么样?如果客户端不读取,服务器怎么能告诉它任何信息?
or it's free again. However, I'm curious to know how the TCP by the default would behave in this scenario.
见上文。