sockets - 关闭/关闭对 TCP 接收缓冲区的影响

标签 sockets networking tcp shutdown

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

  1. 在服务器调用读取之前,客户端在此连接上调用closeFIN 已经到达服务器 TCP,服务器 TCP 发送了 ACK。此时,如果服务器在这个套接字上调用read,它会返回什么?它是否返回 TCP 接收缓冲区中的未读数据?还是只返回0表示EOF,未读数据被丢弃?
  2. 在服务器调用read 之前,客户端在此连接上使用SHUT_WR 调用shutdownFIN 已经到达服务器 TCP,服务器 TCP 发送了 ACK。此时,如果服务器在这个套接字上调用read,它会返回什么?它是否返回 TCP 接收缓冲区中的未读数据?还是只返回0表示EOF,未读数据被丢弃?
  3. 在服务器调用read 之前,客户端只是退出而不调用closeshutdown。客户端主机中的TCP 将发送FIN,此FIN 已到达服务器TCP,并且服务器TCP 已发送ACK。此时,如果服务器在这个套接字上调用read,它会返回什么?它是否返回 TCP 接收缓冲区中的未读数据?还是只返回0表示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?

同上。

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

关于sockets - 关闭/关闭对 TCP 接收缓冲区的影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27937374/

相关文章:

c - Linux C stream socket中的TCP机制

c++ - c/c++ 中的网络拓扑

c++ - 非阻塞套接字,即使没有明确将它们设置为非阻塞

sockets - libevent的基于evhttp的服务器打开的文件太多

c++ - 如何正确执行 OpenSSL 证书?

php - 验证 IP 和 CIDR 组合

java - 在 Java 中使用 UDP 跨网络发送对象

java - TCP 打洞 Java 示例

python - 多线程 TCP 套接字

c - 套接字文件系统与普通磁盘文件系统