Article View: news.software.nntp
Article #56232Re: How recv() determines the receiving is finished?
From: Russ Allbery
Date: Sun, 27 Jan 2002 17:50
Date: Sun, 27 Jan 2002 17:50
30 lines
2367 bytes
2367 bytes
Joshua <JoshuaIIII@hotmail.com> writes:
> I think if input stream has any data,recv() will get some of them and
> return the size into received_len.But if the input stream is
> empty,recv() will block there untill new inputing data is found. So,the
> do/while maybe never has a chance to exit,unless some socket error
> issues.
> And now,I am still not sure how to determine to end the receiving when I
> use recv() to receive data from input stream.
When the remote side closes the connection, read() on that socket (and the
same for recv() if you're using it like read(), which you are here) will
return 0. That's how you know the other side closed the connection.
I highly recommend a good book on Unix network programming, since quite a
lot of this isn't entirely obvious and can be difficult to learn piecemeal
from manual pages. I'm surprised that whatever book you got that code
from didn't explain it.
_Unix Network Programming, Volume One_ by W. Richard Stevens is the most
frequently cited book from which to learn these sorts of things; I highly
recommend it. (Volume one is network sockets and volume two is IPC.)
--
Russ Allbery (rra@stanford.edu) <http://www.eyrie.org/~eagle/>
Please post questions rather than mailing me directly.
<http://www.eyrie.org/~eagle/faqs/questions.html> explains why.
Technical Details
Message-ID:
<yly9iju1i5.fsf@windlord.stanford.edu>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nntp.mbox.zip!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!nntp.stanford.edu!not-for-mailReferences:
<a30rss$f0j$1@mail.cn99.com>Show Full Headers
Complete Article Headers
Message-ID: <yly9iju1i5.fsf@windlord.stanford.edu> Subject: Re: How recv() determines the receiving is finished? Date: Sun, 27 Jan 2002 09:50:26 -0800 From: Russ Allbery <rra@stanford.edu> References: <a30rss$f0j$1@mail.cn99.com> Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nntp.mbox.zip!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!nntp.stanford.edu!not-for-mail Newsgroups: news.software.nntp Organization: The Eyrie Lines: 29 NNTP-Posting-Host: windlord.stanford.edu Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: news.Stanford.EDU 1012153826 2927 171.64.13.23 (27 Jan 2002 17:50:26 GMT) X-Complaints-To: news@news.stanford.edu User-Agent: Gnus/5.090005 (Oort Gnus v0.05) XEmacs/21.4 (Common Lisp, sparc-sun-solaris2.6) Cancel-Lock: sha1:zpXiObry3Ay/WbVbtjsbBqqWkOI=