检测无阻塞插座上的闭合连接 [英] Detect closed connection on non blocking socket

查看:60
本文介绍了检测无阻塞插座上的闭合连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

非常抱歉,如果我的问题是重复的,但是我没有在网站上找到有用的信息.

I'm really sorry if my question is a duplicate, but I didn't find useful infos in the site.

我正在使用非阻塞套接字和select().如何检测客户端是否在非阻塞套接字上关闭了连接?我看到当没有可用数据读取以及连接关闭时,read()会以errno = EWOULDBLOCK返回-1.

I'm using non blocking sockets and a select(). How can I detect if a client closed the connection on non-blocking socket? I saw that a read() returns -1 with errno = EWOULDBLOCK when no datas are available to be read and also when a connection is closed.

如何区分上述情况?

推荐答案

对等方关闭连接后:

  1. select()将返回可读的套接字.
  2. 套接字上的 recv() read()将返回零.
  1. select() will return the socket as readable.
  2. A recv() or read() on the socket will return zero.

我看到当没有可用数据读取时,read()返回-1且errno = EWOULDBLOCK

I saw that a read() returns -1 with errno = EWOULDBLOCK when no datas are available to be read

正确,但连接未关闭.

以及连接关闭时.

and also when a connection is closed.

不,你没有.那是不对的.它返回零.

No you didn't. That's not correct. It returns zero.

如何区分上述情况?

How can I discriminate above cases?

它们是不同的,并且它们以不同的方式表现出来.

They aren't the same, and they don't manifest themselves in the same way.

这篇关于检测无阻塞插座上的闭合连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆