close/shutdown 对 TCP 接收缓冲区的影响

The effect close/shutdown has on the TCP receive buffer

假设我有一个 TCP 客户端和一个 TCP 服务器。首先,客户端向服务器发送了一些数据,但是服务器正忙于做其他事情,所以它没有读取这个连接。数据保存在服务器主机上的 TCP 接收缓冲区中。在这一点上,请帮助我澄清在以下不同情况下会发生什么:

  1. 在服务器调用 read 之前,客户端在此连接上调用 closeFIN 已到达服务器 TCP,服务器 TCP 已发送 ACK。此时,如果服务器在这个套接字上调用read,它会return什么呢? return TCP 接收缓冲区中是否有未读数据?还是直接return0表示EOF,未读数据丢弃?
  2. 在服务器调用 read 之前,客户端在此连接上用 SHUT_WR 调用 shutdownFIN 已到达服务器 TCP,服务器 TCP 已发送 ACK。此时,如果服务器在这个套接字上调用read,它会return什么呢? return TCP 接收缓冲区中是否有未读数据?还是直接return0表示EOF,未读数据丢弃?
  3. 在服务器调用 read 之前,客户端直接退出而不调用 closeshutdown。而客户端主机的TCP会发送FIN,这个FIN已经到达服务器TCP,服务器TCP已经发送ACK。此时,如果服务器在这个套接字上调用read,它会return什么呢? return TCP 接收缓冲区中是否有未读数据?还是直接return0表示EOF,未读数据丢弃?

Before server call read, client calls close on this connection. And the FIN has arrived in server TCP, and server TCP has sent ACK. So at this point, if server calls read on this socket, what will read return? Does it return the unread data in TCP receive buffer?

是的。

Or does it just return 0 to indicate EOF, and the unread data is discarded?

没有

Before server call read, client calls shutdown with SHUT_WR on this connection. And the FIN has arrived in server TCP, and server TCP has sent ACK. So at this point, if server calls read on this socket, what will read return? Does it return the unread data in TCP receive buffer? Or does it just return 0 to indicate EOF, and the unread data is discarded?

同上

Before server call read, client just exit without calling either close or shutdown. And TCP in client host will send the FIN, this FIN has arrived in server TCP, and server TCP has sent ACK. So at this point, if server calls read on this socket, what will read return? Does it return the unread data in TCP receive buffer? Or does it just return 0 to indicate EOF, and the unread data is discarded?

同上

清除接收缓冲区的唯一方法是读取所有数据,或者如果服务器收到 reset,即 RST,这可以是以各种方式引起,或者当然如果服务器在没有读取所有数据的情况下关闭套接字,在这种情况下它 发出 一个 RST 如果有未决的读取数据。