Reading data from network stream

K

kjell

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

The problem with this code is that it will hang until a full 1000 bytes
has been read before processing the data. Is there a way I can modify
these code so that will process the data as soon as it becomes
available?

Thanks for your help,
Kjell
 
W

Walter Roberson

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) ]
 
C

Chris Torek

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

Ancient_Hacker

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top