473,721 Members | 2,186 Online
Bytes | Software Development & Data Engineering Community
+ 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 5398
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.FromStrea m() 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.com wrote:
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.co m>
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*********@Nn OwSlPiAnMk.comw rote in message
news:13******** *****@corp.supe rnews.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.FromStrea m() 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
1727
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 binascii module without any luck. Best regards, -fredrik-normann-
2
3066
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 disk. My XML files have the following structure: <?xml version="1.0" encoding="utf-8" ?> <imagepla xmlns:dt="urn:schemas-microsoft-com:datatypes">
20
3065
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: --cut here-- typedef struct pkg_ { short int info; char* data;
1
3675
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 know the size of the file, so how can I check the end of the file. Is there a way for my client to know if the connexion was properly close.
8
7450
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 want to open it and read its content and copy to the file which is created on the harddisk. How can I do this. fgets will require specifica no allocated to buffer. Please check my logic . please dont mind syntax or improper function names. I tried...
4
3849
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) linked list to continousely (dynamically) free up more memory when needed and put the buffers with the data of the file on a sort of stack. The problem is that i have no idea how to do this.
21
3821
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 Second file is from NewLine to end of file. Kindly let me know whether this si possible Thanks Rohith
0
1438
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 expect. Specifically, in the code below where the error occurs, line == ''. Python 2.4.1 (#2, Oct 12 2005, 01:36:32) 20050518] on freebsd6 Type "help", "copyright", "credits" or "license" for more information. >>> import urllib2
6
5269
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
8840
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8730
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9131
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9064
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8007
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6669
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5981
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3189
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 we have to send another system
3
2130
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.