🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: news.software.nntp
11 messages
11 total messages Started by Joshua Sun, 27 Jan 2002 12:29
How recv() determines the receiving is finished?
#56230
Author: Joshua
Date: Sun, 27 Jan 2002 12:29
40 lines
2322 bytes
I read these codes from a book. I am not sure whether it can work:

char received_buffer[128];
int received_len;

do
{
    received_len=recv(socket,received_buffer,sizeof(received_buffer),0);
    if (SOCKET_ERROR==received_len)    //socket error or session is broken
        break;
    received_buffer[received_len]=0;
    printf(received_buffer);
}
while (0!=received_len);

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. Is it the only way to get anything
from inputing stream and scan the whole inputing data for a specific string
which is given by the server? If the inputing data is large,I have to
prepare a large buffer for containing the whole data, and a very complicated
arithmetic to search the ending-string. I think there must be some
ready-made sample codes for that. Anyone would give me some of them or idea
for that?Thanks.











Re: How recv() determines the receiving is finished?
#56232
Author: Russ Allbery
Date: Sun, 27 Jan 2002 17:50
30 lines
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.
Re: How recv() determines the receiving is finished?
#56235
Author: Joshua
Date: Sun, 27 Jan 2002 19:25
49 lines
3328 bytes
Thank you,Russ.

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.

Thanks for your recommanding on the reference book. This time I am writing a
little test tool for News Server,maybe these sort of books are too heavy for
me,at least now. I want to make it work firstly.blocked by only this little
question.Could you give me more detail about that? Some pieces of sample
codes are will be the best.

"Russ Allbery" <rra@stanford.edu> wrote in message
news:yly9iju1i5.fsf@windlord.stanford.edu...
> 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.)




Re: How recv() determines the receiving is finished?
#56236
Author: 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.
Re: How recv() determines the receiving is finished?
#56237
Author: Russ Allbery
Date: Sun, 27 Jan 2002 21:12
18 lines
1823 bytes
Russ Allbery <rra@stanford.edu> writes:

> 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.

You don't actually *need* non-blocking I/O, I should add.  It's just
easier to think about that way in my opinion.  The basic idea is that you
use select to check your file descriptors and wait for something to
happen, and you don't try to read until select says that there's data
waiting.

-- 
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.
Re: How recv() determines the receiving is finished?
#56239
Author: Andrew Gierth
Date: Sun, 27 Jan 2002 22:05
31 lines
2540 bytes
>>>>> "Joshua" == Joshua  <JoshuaIIII@hotmail.com> writes:

 Joshua> Thank you,Russ.

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

For NNTP, the client knows whether it's expecting a single-line or a
multi-line response. It reads the first line (status line) and, if it's
a success response and a multi-line response is expected, then it reads
following lines until it gets a line consisting only of '.'+CRLF. This
is all explained in the RFC.

The example you gave is just a simple "read until connection closed"
loop (with, I might add, a security bug of the "format string" type),
and clearly has nothing to do with NNTP. I suggest you throw away
whatever book you got it from, because an author who writes
    printf(received_buffer);
is almost certain to have made other equally serious errors.

-- 
Andrew.
Re: How recv() determines the receiving is finished?
#56241
Author: Joshua
Date: Mon, 28 Jan 2002 02:25
15 lines
1659 bytes
OK, I will check them by your advice.

"Russ Allbery" <rra@stanford.edu> wrote in message
news:ylsn8ro9o0.fsf@windlord.stanford.edu...
> Joshua <JoshuaIIII@hotmail.com> writes:

> > There must be other way for this.
>
> Ah, now I understand what you're looking for.
> 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.


Re: How recv() determines the receiving is finished?
#56244
Author: Joshua
Date: Mon, 28 Jan 2002 02:39
26 lines
2383 bytes
Your help is very help.Thank you so much but...

I didn't understand you very much here.Coz I have tried and I think
"printf(received_buffer);" is working just well. I couldn't find what's
wrong with this line of code.Could you point it out to me?Thank you!


"Andrew Gierth" <andrew@erlenstar.demon.co.uk> wrote in message
news:87665ntppq.fsf@erlenstar.demon.co.uk...
> >>>>> "Joshua" == Joshua  <JoshuaIIII@hotmail.com> writes:

> For NNTP, the client knows whether it's expecting a single-line or a
> multi-line response. It reads the first line (status line) and, if it's
> a success response and a multi-line response is expected, then it reads
> following lines until it gets a line consisting only of '.'+CRLF. This
> is all explained in the RFC.
>
> The example you gave is just a simple "read until connection closed"
> loop (with, I might add, a security bug of the "format string" type),
> and clearly has nothing to do with NNTP. I suggest you throw away
> whatever book you got it from, because an author who writes
>     printf(received_buffer);
> is almost certain to have made other equally serious errors.


Re: How recv() determines the receiving is finished?
#56246
Author: Joshua
Date: Mon, 28 Jan 2002 02:43
19 lines
2005 bytes
Yes,I think I don't need non-blocking I/O. I think maybe I don't need
'select' function. Andrew's idea sounds good. I am trying that.

"Russ Allbery" <rra@stanford.edu> wrote in message
news:yl4rl7zeel.fsf@windlord.stanford.edu...
> Russ Allbery <rra@stanford.edu> writes:
>
> > 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.
>
> You don't actually *need* non-blocking I/O, I should add.  It's just
> easier to think about that way in my opinion.  The basic idea is that you
> use select to check your file descriptors and wait for something to
> happen, and you don't try to read until select says that there's data
> waiting.


Re: How recv() determines the receiving is finished?
#56245
Author: Andrew Gierth
Date: Mon, 28 Jan 2002 04:18
12 lines
1490 bytes
>>>>> "Joshua" == Joshua  <JoshuaIIII@hotmail.com> writes:

 Joshua> Your help is very help.Thank you so much but...

 Joshua> I didn't understand you very much here.Coz I have tried and I
 Joshua> think "printf(received_buffer);" is working just well.

Try it with data that contains several "%s" sequences.

-- 
Andrew.
Re: How recv() determines the receiving is finished?
#56247
Author: Joshua
Date: Mon, 28 Jan 2002 05:55
18 lines
1724 bytes
Oh,you are right. I am so stupid.Sorry about it.

"Andrew Gierth" <andrew@erlenstar.demon.co.uk> wrote in message
news:87bsffqfb7.fsf@erlenstar.demon.co.uk...
> >>>>> "Joshua" == Joshua  <JoshuaIIII@hotmail.com> writes:
>
>  Joshua> Your help is very help.Thank you so much but...
>
>  Joshua> I didn't understand you very much here.Coz I have tried and I
>  Joshua> think "printf(received_buffer);" is working just well.
>
> Try it with data that contains several "%s" sequences.
>
> --
> Andrew.


Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads