🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: news.software.nntp
Article #56236

Re: How recv() determines the receiving is finished?

#56236
From: Russ Allbery
Date: Sun, 27 Jan 2002 19:50
31 lines
2564 bytes
Joshua <JoshuaIIII@hotmail.com> writes:

> In fact, I knew what happens when the remote side closes the
> connection. I only don't know when the input stream is ending.  The News
> client and Server may run require-respond many times in every single
> socket session.The client send require command such as "mode reader",and
> waits the server's respond. Here,client uses recv() to get the
> responding data. The session will keep open, so client can not determine
> the ending of the responding data by estimating "return 0".Before the
> session is closed by any side,the recv() will generally not return 0.

> There must be other way for this.

Ah, now I understand what you're looking for.

Yes, recv() normally blocks until it gets data.  If you don't want that
behavior, you need to use non-blocking I/O and a select() loop to wait for
new data.  That's a bit complex to explain in a Usenet posting, but any
good book on network programming should go into this in great detail since
it's an extremely common programming idiom for servers.

I don't have a simple example, but you can look at the main loop of most
servers and see something involving non-blocking I/O and a select loop.
See, for instance CHANreadloop() in innd/chan.c in the INN source tree.

-- 
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: <ylsn8ro9o0.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-mail
References: <a30rss$f0j$1@mail.cn99.com> <yly9iju1i5.fsf@windlord.stanford.edu> <a31khk$895$1@mail.cn99.com>

Show Full Headers
Complete Article Headers
Message-ID: <ylsn8ro9o0.fsf@windlord.stanford.edu>
Subject: Re: How recv() determines the receiving is finished?
Date: Sun, 27 Jan 2002 11:50:39 -0800
From: Russ Allbery <rra@stanford.edu>
References: <a30rss$f0j$1@mail.cn99.com> <yly9iju1i5.fsf@windlord.stanford.edu> <a31khk$895$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: 30
NNTP-Posting-Host: windlord.stanford.edu
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
X-Trace: news.Stanford.EDU 1012161040 2927 171.64.13.23 (27 Jan 2002 19:50:40 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:WappHco+Wr4W4xmAXT7RN5LbV+A=