473,408 Members | 2,839 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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 4002
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 (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.
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: bidou | last post by:
Hello World, Using (unmanaged) C++ only, I'd like to be able to read a binary stream coming from a .net object standard serialization. For example, let's assume I have a .net client which...
3
by: Nick | last post by:
I have found a class that compresses and uncompresses data but need some help with how to use part of it below is the deflate method which compresses the string that I pass in, this works OK. At...
16
by: Ali | last post by:
Hi I want to write (or read) to a stream, but the data is not byte array I converted the data to byte array manually, but it is very slow, (becuse the data is very large) Is another way for this...
21
by: JoKur | last post by:
Hello, First let me tell you that I'm very new to C# and learning as I go. I'm trying to write a client application to communicate with a server (that I didn't write). Each message from the...
3
by: Adman | last post by:
Hi all. I've done some websearching, and haven't been able to find my question answered, so I thought I'd post. I apologize if this has already been answered. My question seems to be a...
3
by: Alex Clark | last post by:
Hi All, I'm having some problems reading a network stream. I'm writing a lightweight POP client to handle a very specific task, but I keep unexpectedly reaching the end of the datastream when...
3
by: Sir Psycho | last post by:
Hi, For some reason, when i step over this code, it returns the full byte stream im expecting from the server, however when I let it run with no intervention, it only seems to grab a small chunk...
8
by: Peter Bradley | last post by:
Hi, I wonder if anyone can help me out? I'm trying to implement an EPP (rfc4934 and rfc4930) client. So far I've managed to connect and authorise using an X509 Certificate. This should...
10
by: puzzlecracker | last post by:
Say I want to arrange bytes in the internal buffer in a certain way. I receive those bytes in the socket. One solution is to read in socket in pieces: byte buffer = new byte; int index = 0;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.