问题描述
我知道 read() 是一个阻塞调用,除非我使套接字非阻塞.所以我希望请求 4K 数据的 read() 调用应该返回一个正值(读取的字节数)或错误时返回 -1(客户端可能重置连接等).我的问题是: read() 在任何情况下都可以返回0"吗?
I know that read() is a blocking call unless I make the socket non-blocking. So I expect read() call which requests 4K of data should return a positive value ( no of bytes read) or -1 on error ( possible connection reset by client etc). My question is: Can read() return '0' on any occasion?
我正在以这种方式处理 read():
I am handling the read() this way:
if ((readval = read(acceptfd, buf, sizeof(buf) - 1)) < 0)
{
}
else
{
buf[readval] = 0;
//Do some thing with data
}
如果 read() 返回零并且我知道如何修复它,则此代码会爆炸.但是 read() 有可能返回零吗?
This code bombs if read() return zero and I know how to fix it. But is it possible for read() to return zero?
推荐答案
当一个 TCP 连接在一侧关闭时 read() 在另一侧返回 0 字节.
When a TCP connection is closed on one side read() on the other side returns 0 byte.
这篇关于已连接套接字上的 read() 函数可以返回零字节吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!