Groups | Blog | Home
all groups > c# > september 2007 >

c# : Reading big chunk of binary data from socket



Peter Duniho
9/28/2007 12:59:42 PM
[quoted text, click to view]

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.

Jon Skeet [C# MVP]
9/28/2007 8:33:08 PM
[quoted text, click to view]

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 - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Willy Stevens
9/28/2007 10:33:09 PM
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


Willy Stevens
9/28/2007 11:32:31 PM
Hello and thanks for both,

[quoted text, click to view]

Yes it's TCP so this approach suites well for me,

Thanks


[quoted text, click to view]

AddThis Social Bookmark Button