473,480 Members | 2,094 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Reading big chunk of binary data from socket

Hello,

In my application I have to read sometimes quite big chunk of binary data.
I have a buffer which default size is 32000 bytes.

But how could I read binary data that exceeds 32000 bytes?
Is allocating more size to buffer and copy data that exceeds 32000 bytes at
tail the only way
or are there any other possibilities?

Cheers W

Sep 28 '07 #1
3 5348
Willy Stevens wrote:
Hello,

In my application I have to read sometimes quite big chunk of binary data.
I have a buffer which default size is 32000 bytes.

But how could I read binary data that exceeds 32000 bytes?
Is allocating more size to buffer and copy data that exceeds 32000 bytes at
tail the only way
or are there any other possibilities?
What kind of socket?

If it's a UDP or other message-oriented protocol, you need to provide a
buffer large enough to contain the message. Some protocols or service
providers may support partial messages via the Partial socket flag
(MSG_PARTIAL in Winsock), but I would say it's better to just provide a
large enough buffer rather than relying on that flag being usable.

For TCP or other stream-oriented protocols, you simply read data in
chunks as large as you feel you can reasonably handle. So if your
buffer is only 32K, you just read 32K at a time until you have extracted
all of the data from the stream.

What you do with the data after each read depends on what you're going
to do with it later. For example, you might be reading a JPEG file that
you want to display, so you'd have to allocate a data structure large
enough to hold all of the JPEG data and iteratively copy the data you
read from the Socket into that data structure. As an example, you might
create a MemoryStream, initializing its size to be equal to the size of
data you're going to read, and just write the read data chunks to the
MemoryStream instance. Then you can use the MemoryStream as the
parameter for Image.FromStream() to create the JPEG.

On the other hand, if it's data you want to save to a file, you'd
iteratively read the data from the Socket and write the new data to the
file with each read.

Pete
Sep 28 '07 #2
Willy Stevens <ws@unknown.comwrote:
In my application I have to read sometimes quite big chunk of binary data.
I have a buffer which default size is 32000 bytes.

But how could I read binary data that exceeds 32000 bytes?
Is allocating more size to buffer and copy data that exceeds 32000 bytes at
tail the only way or are there any other possibilities?
Well, you could create a list of byte arrays, reading one byte array at
a time. The easiest way would just be to use a stream abstraction over
the socket, however, and just keep reading until you're finished.

See http://pobox.com/~skeet/csharp/readbinary.html for some caveats.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 28 '07 #3
Hello and thanks for both,
>For TCP or other stream-oriented protocols, you simply read data in chunks
as large as you feel you can reasonably handle. So if your buffer is only
32K, you just read 32K at a time until you have extracted all of the data
from the stream.
Yes it's TCP so this approach suites well for me,

Thanks
"Peter Duniho" <Np*********@NnOwSlPiAnMk.comwrote in message
news:13*************@corp.supernews.com...
Willy Stevens wrote:
>Hello,

In my application I have to read sometimes quite big chunk of binary
data.
I have a buffer which default size is 32000 bytes.

But how could I read binary data that exceeds 32000 bytes?
Is allocating more size to buffer and copy data that exceeds 32000 bytes
at tail the only way
or are there any other possibilities?

What kind of socket?

If it's a UDP or other message-oriented protocol, you need to provide a
buffer large enough to contain the message. Some protocols or service
providers may support partial messages via the Partial socket flag
(MSG_PARTIAL in Winsock), but I would say it's better to just provide a
large enough buffer rather than relying on that flag being usable.

For TCP or other stream-oriented protocols, you simply read data in chunks
as large as you feel you can reasonably handle. So if your buffer is only
32K, you just read 32K at a time until you have extracted all of the data
from the stream.

What you do with the data after each read depends on what you're going to
do with it later. For example, you might be reading a JPEG file that you
want to display, so you'd have to allocate a data structure large enough
to hold all of the JPEG data and iteratively copy the data you read from
the Socket into that data structure. As an example, you might create a
MemoryStream, initializing its size to be equal to the size of data you're
going to read, and just write the read data chunks to the MemoryStream
instance. Then you can use the MemoryStream as the parameter for
Image.FromStream() to create the JPEG.

On the other hand, if it's data you want to save to a file, you'd
iteratively read the data from the Socket and write the new data to the
file with each read.

Pete

Sep 28 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1713
by: Fredrik Normann | last post by:
Hello, I'm trying to read the binary files under /var/spool/rwho/ so I'm wondering if anyone has done that before or could give me some clues on how to read those files. I've tried to use the...
2
3039
by: jimmyfishbean | last post by:
Hi, I am using VB6, SAX (implementing IVBSAXContentHandler). I need to extract binary encoded data (images) from large XML files and decode this data and generate the appropriate images onto...
20
3010
by: ishmael4 | last post by:
hello everyone! i have a problem with reading from binary file. i was googling and searching, but i just cant understand, why isnt this code working. i could use any help. here's the source code:...
1
3637
by: cyril | last post by:
Hi there, I wonder how to send and receive big data (by chunk) with an unknown size in an opened socket in C. For example, my server can send to my client a big binary file. My client didn't...
8
7432
by: Vijay | last post by:
Hi. I have a binary file which is on a CD it has filesize around 300 MB. Its not a text file. I want to read it and copy its content to a new file on hard disk. I dont want to use filecopy. I...
4
3822
by: Henk | last post by:
Hi, I am new to the c-programming language and at the moment I am struggling with the following: I want to read a file using fread() and then put it in to memory. I want to use a (singel)...
21
3788
by: Rohith | last post by:
I need to split a large binary file into two binary files. I have a delimiter (say NewLine) in the binaryfile. I need to split the binary file such that the first file is upto the NewLine and the...
0
1418
by: philip20060308 | last post by:
Hi all, Has anyone ever seen Python 2.4.1's httplib choke when reading chunked content? I'm using it via urrlib2, and I ran into a particular server that returns something that httplib doesn't...
6
5234
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
0
6915
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
7097
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5353
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4794
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3003
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2993
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1307
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
567
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.