468,484 Members | 1,757 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,484 developers. It's quick & easy.

Reading data from network stream

Hi,

I'm trying to write a program that reads data from a network stream.
I would like the program to read all available data in the buffer and
then process the data. I do not want the program to hang unless there
is no data in the buffer. For example if there are ten bytes available
in the buffer I would like the program to read those ten bytes and then
processed the data. If there are twenty bytes available in the buffer
I would like the program to read those twenty bytes and then process
them. My implementation looks something like this:

// Allocate the buffer
const unsigned long nBlockSize = 1000;
unsigned char pData[nBlockSize];

// Get data
while (true)
{
size_t sizeBytesRead;
sizeBytesRead = fread(pData, 1, nBlockSize, m_pFileInStream);

// Process the data
Sep 22 '06 #1
3 3759
In article <11**********************@i3g2000cwc.googlegroups. com>,
<kj***@cablescan.comwrote:
>I'm trying to write a program that reads data from a network stream.
I would like the program to read all available data in the buffer and
then process the data. I do not want the program to hang unless there
is no data in the buffer. For example if there are ten bytes available
in the buffer I would like the program to read those ten bytes and then
processed the data. If there are twenty bytes available in the buffer
I would like the program to read those twenty bytes and then process
them.
You can't do that in standard C. Standard C does not offer any method
to check to see whether input is waiting, other than trying to read
it and ending up waiting for it.

Standard C does not know anything about network streams, which
suggests that you might be operating in an environment that is
augmented with additional functionality. If so, your environment
might offer you a way to do what you want. You should consult
a newsgroup more specific to your platform.
[(e.g., you might want to examine POSIX's,
fcntl(fileno(pData),F_SETFL,FNONBLK) ]
--
Programming is what happens while you're busy making other plans.
Sep 23 '06 #2
>In article <11**********************@i3g2000cwc.googlegroups. com>
<kj***@cablescan.comwrote:
>>I'm trying to write a program that reads data from a network stream.
I would like the program to read all available data in the buffer and
then process the data. I do not want the program to hang unless there
is no data in the buffer. ...
In article <ef**********@canopus.cc.umanitoba.ca>
Walter Roberson <ro******@ibd.nrc-cnrc.gc.cawrote:
>You can't do that in standard C. Standard C does not offer any method
to check to see whether input is waiting, other than trying to read
it and ending up waiting for it.

Standard C does not know anything about network streams, which
suggests that you might be operating in an environment that is
augmented with additional functionality. If so, your environment
might offer you a way to do what you want. You should consult
a newsgroup more specific to your platform.
Indeed.
>[(e.g., you might want to examine POSIX's,
fcntl(fileno(pData),F_SETFL,FNONBLK) ]
But this is, I think, not going to be the right solution. (In
particular, combining O_NONBLOCK [not FNONBLK] with fread() is
a bad idea, on POSIX systems.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Sep 24 '06 #3

kj***@cablescan.com wrote:
Hi,

I'm trying to write a program that reads data from a network stream.
I would like the program to read all available data in the buffer and
then process the data.
The C language doesnt have much of a concept of "time", especially
"real-time".

So this question is far off-topic for this n.g.

That said, you might check to see if your C has a "select()" or
"non-blocking I/O", or "sockets" feature. Those frobs let you start
an I/O operaton and not have to wait for completion, or let you check
to see how much data is available at that instant.

Sep 24 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by Ali | last post: by
21 posts views Thread by JoKur | last post: by
3 posts views Thread by Adman | last post: by
3 posts views Thread by Alex Clark | last post: by
3 posts views Thread by Sir Psycho | last post: by
8 posts views Thread by Peter Bradley | last post: by
10 posts views Thread by puzzlecracker | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.