473,395 Members | 1,652 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,395 software developers and data experts.

dynamic buffer size

Hi, I'm writing an app in bluetooth & wi-fi. It is client/server on
both. We have defined our own network protocol that should run over
both mediums. There are packet definitions of variable length.

Inside the packet, one field indicates its length. However, I don't
know ahead of time which packet shall be coming in when I read.

I'm using asynchronous read methods for both. In sockets, I do
Socket.BeginRead( ). This method must be supplied with a byte buffer,
and you must also specify the length to read as one of the parameters.

In the bluetooth, I do the same thing except with serial port profile
using Stream.BeginRead( ).

The problem thus becomes I must specify a byte buffer that is larger
than the packet to ensure I get it all. This causes annoying
post-processing where I must then parse the packet and truncate the
buffer to its real size.

I also don't like this b/c of possible buffer overflow.

My question is simply: is there a better way to do this?

Jan 3 '07 #1
1 3982

call BeginRead with a buffer big enough to hold the header. Then
parse out just the header to get the length. Then call BeginRead
again to read the rest of the packet with an appropriately sized
buffer.

However, it may not save you any time or memory to do that since using
a consistent size would provide for more efficient memory
allocation/deallocation. I'd run a profiler on the code to see how
performance and memory usage are before actually worrying about a
problem.

You could also experiment with keeping the buffers and reusing them,
but that can certainly lead to problems if you're not careful and
again may not be more efficient.

Also, you don't have to worry about memory overruns with BeginRead
calls--.NET will ensure that it only reads up to the length of the
buffer. In fact relying on header information to allocate memory and
then reading that amount of bytes is exactly what can lead to memory
overruns.

HTH,

Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On 3 Jan 2007 06:48:42 -0800, "davis" <da*******@gmail.comwrote:
>Hi, I'm writing an app in bluetooth & wi-fi. It is client/server on
both. We have defined our own network protocol that should run over
both mediums. There are packet definitions of variable length.

Inside the packet, one field indicates its length. However, I don't
know ahead of time which packet shall be coming in when I read.

I'm using asynchronous read methods for both. In sockets, I do
Socket.BeginRead( ). This method must be supplied with a byte buffer,
and you must also specify the length to read as one of the parameters.

In the bluetooth, I do the same thing except with serial port profile
using Stream.BeginRead( ).

The problem thus becomes I must specify a byte buffer that is larger
than the packet to ensure I get it all. This causes annoying
post-processing where I must then parse the packet and truncate the
buffer to its real size.

I also don't like this b/c of possible buffer overflow.

My question is simply: is there a better way to do this?
Jan 3 '07 #2

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

Similar topics

4
by: Scott Lyons | last post by:
Hey all, Can someone help me figure out how to pass a dynamic array into a function? Its been giving me some trouble, and my textbook of course doesnt cover the issue. Its probably something...
4
by: Vincent Berkeley | last post by:
Is there a way to allocate memory for a character array without knowing in advance what size you need? For instance, is there a way to allocate memory based on the length of an input string after...
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
5
by: swarsa | last post by:
Hi All, I realize this is not a Palm OS development forum, however, even though my question is about a Palm C program I'm writing, I believe the topics are relevant here. This is because I...
4
by: andychambers2002 | last post by:
I'm working on a "TempFile" class that stores the data in memory until it gets larger than a specified threshold (as per PEP 42). Whilst trying to implement it, I've come across some strange...
26
by: Andrew Poelstra | last post by:
I hacked this together this morning so that I could shift my out-of- space code away from the rest of my logic. I wanted to allow array syntax on my dynamic buffers, so I manually created a struct...
13
by: coosa | last post by:
Dear all, Using the conio implementation i wanted to create a dynamic string, whereby its size would be determined after each keyboard hit; in other words, i don't want to ask the user to...
1
by: pete m | last post by:
I would like to support stdio functions for an extremely primitive type system, as shown in the attached sample program, using dynamic creation of a va_list. I've tested it on successfully a...
14
by: dopiotr | last post by:
Hello Group, int Msg; MPI_Send(Msg, size, MPI_INT, DEST, TAG, MPI_COMM_WORLD); this is working fine. But how to send Msg declared like this: int *Msg=(int*)malloc(sizeof(int)*size); It...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.