检测无阻塞插座上的闭合连接 [英] Detect closed connection on non blocking socket
问题描述
非常抱歉,如果我的问题是重复的,但是我没有在网站上找到有用的信息.
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.
如何区分上述情况?
推荐答案
对等方关闭连接后:
-
select()
将返回可读的套接字. - 套接字上的
recv()
或read()
将返回零.
select()
will return the socket as readable.- A
recv()
orread()
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屋!