Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Reading data from network stream

Reply
Thread Tools

Reading data from network stream

 
 
kjell@cablescan.com
Guest
Posts: n/a
 
      09-22-2006
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

 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      09-23-2006
In article <. com>,
<> wrote:
>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.
 
Reply With Quote
 
 
 
 
Chris Torek
Guest
Posts: n/a
 
      09-24-2006
>In article <. com>
> <> wrote:
>>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 <ef286l$19n$>
Walter Roberson <> wrote:
>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 (40°39.22'N, 111°50.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.
 
Reply With Quote
 
Ancient_Hacker
Guest
Posts: n/a
 
      09-24-2006

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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Buffered reading seems to corrupt data stream markspace Java 6 11-07-2011 02:50 AM
Reading Java byte[] data stream over standard input sapsi Python 6 05-19-2008 01:21 PM
reading byte stream from network akifusenet@gmail.com Ruby 5 08-22-2007 11:07 AM
How to GET multi-word input from a *file* stream as opposed to a *console* stream? sherifffruitfly@gmail.com C++ 9 04-27-2006 04:14 PM
Doing readline in a thread from a popen4('rsync ...') stream blocks when the stream ends. Rasmusson, Lars Python 1 04-30-2004 08:10 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57