简体   繁体   中英

In C getting a 0 from a read call using a TCP socket means that a FIN was received?

Sometimes I seem to get 0, which means that all I got was a header, does that mean I should assume that I received a FIN and close that socket?

Thanks in advance!

Yes. Zero means, generically, end-of-file. What "end of file" means depends on what kind of file descriptor you are using.

In the context of a TCP socket, the only way that I know of that you can read zero is if a FIN is received. By contrast, if a RST is received read() would return -1 with errno == ECONNRESET .

You can also get a 0 from read even when the socket is still connected and the remote side is not closed. This happens if you pass 0 as count.

I don't know (off the top of my head) whether you must have gotten a FIN packet but you're right on the second count.

A return code of 0 means end of file which, in the context of sockets, means there won't be any more data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM