操作系统提供了什么 API 可以让我们知道一个 socket 连接是否断开了?

操作系统提供了什么 API 可以让我们知道一个 socket 连接是否断开了?

对面四次挥手之后,我怎么知道我的 socket 已经 closed 了呢?

比如有两个通讯主体 A appB appA 调用 close,此时 A 的操作系统向 B 的操作系统发出四次挥手请求。。。。。四次挥手完成。。。。。此时 A 知道 tcp 已经断开了,毕竟是 A 发起的。但是 B 会知道吗?

我的理解是 B 是不知道的,因为 B 是应用层的,四次挥手是操作系统作为底层处理的,不会想上通知 B。

但是操作系统应该开放一个 API 让应用层查询这个 socket 是否已经 close 了呀!有吗?Python 的没有找到!


回答:

recv

Return Value
Upon successful completion, recv() shall return the length of the message in bytes. If no messages are available to be received and the peer has performed an orderly shutdown, recv() shall return 0. Otherwise, -1 shall be returned and errno set to indicate the error.


回答:

B应用比A关得还早,为什么会不知道?在TCP没有关闭前,上层应用又不会关闭。

以上是 操作系统提供了什么 API 可以让我们知道一个 socket 连接是否断开了? 的全部内容, 来源链接: utcz.com/p/938342.html

回到顶部