开发者

Can read() function on a connected socket return zero bytes?

开发者 https://www.devze.com 2022-12-22 23:32 出处:网络
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

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?

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  
    }

This code bombs if read() return zero and I know how to fix it. But is it possible for read() to return zero?


When a TCP connection is closed on one side read() on the other side returns 0 byte.

0

精彩评论

暂无评论...
验证码 换一张
取 消