检测非阻塞套接字上的关闭连接
Detect closed connection on non blocking socket
如果我的问题重复,我真的很抱歉,但我没有在网站上找到有用的信息。
我正在使用非阻塞套接字和 select()。如何检测客户端是否关闭了非阻塞套接字上的连接?我看到 read() returns -1 with errno = EWOULDBLOCK 当没有数据可供读取以及连接关闭时。
如何区分以上情况?
当对等方关闭特定套接字的连接时,对该套接字上的 read()
的调用将 return 0
。此行为独立于套接字的阻塞状态。
来自 man 2 read
(斜体 由我):
RETURN VALUE
On success, the number of bytes read is returned (zero indicates end of file)
当对方关闭连接时:
select()
将 return 套接字可读。
- 套接字上的
recv()
或 read()
将 return 为零。
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.
不,你没有。那是不正确的。它 return 为零。
How can I discriminate above cases?
它们不一样,也不以相同的方式表现出来。
如果我的问题重复,我真的很抱歉,但我没有在网站上找到有用的信息。
我正在使用非阻塞套接字和 select()。如何检测客户端是否关闭了非阻塞套接字上的连接?我看到 read() returns -1 with errno = EWOULDBLOCK 当没有数据可供读取以及连接关闭时。
如何区分以上情况?
当对等方关闭特定套接字的连接时,对该套接字上的 read()
的调用将 return 0
。此行为独立于套接字的阻塞状态。
来自 man 2 read
(斜体 由我):
RETURN VALUE
On success, the number of bytes read is returned (zero indicates end of file)
当对方关闭连接时:
select()
将 return 套接字可读。- 套接字上的
recv()
或read()
将 return 为零。
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.
不,你没有。那是不正确的。它 return 为零。
How can I discriminate above cases?
它们不一样,也不以相同的方式表现出来。