By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,968 Members | 1,843 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,968 IT Pros & Developers. It's quick & easy.

Reading data from network stream

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
>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

P: n/a

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.