473,671 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.BeginRea d( ). 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.BeginRea d( ).

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 4008

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*******@gmai l.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.BeginRe ad( ). 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.BeginRea d( ).

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
7673
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 simple, but its just not popping into my mind at the moment. My little snippet of code is below. Basically, the studentID array is dynamic so it will fit any length of a Student's Name. What I'm trying to do is place this chunk of code into a...
4
2520
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 the fact? This is a point of some confusion for me, and I don't want to go around creating arrays large enough to cover any contingency. Is there some malloc trick I'm missing? Pardon my newbieness. -- To respond by email, add 1 to the...
8
3673
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 static; if the input file contained more than entries, tough. This time I want to do it right - use a dynamic array that increases in size with each word read from the file. A few test programs that make use of **List and realloc( List, blah...
5
3751
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 believe the problem centers around my handling of strings, arrays, pointers and dynamic memory allocation. Here is the problem I'm trying to solve: I want to fill a list box with a list of Project Names from a database (in Palm this is more...
4
1832
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 behaviour. Can anyone explain this? The test case at the bottom starts a TempFile at size 50 and prints its type. It then increases the size to the threshold at which point "self" is changed to being a TemporaryFile. It seems that the next call...
26
3473
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 with malloc() and judicious use of void* pointers. I decided to post it because stuff like this is breeding ground for UB, and this group is good at finding it. (I had a dentist analogy for that, but it didn't work out.) Here's my code:
13
2030
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 specify the the size, but rather keep him/her typing and after each keyboard hit, the function getch() determines whether he/she entered the ENTER key to end the process; otherwise, increases the dynamic size or also decreases it if the back key was...
1
9065
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 couple platforms, but what I don't know is whether it's actually a legal program. The function of interest is usnprintf, "union sprintf". (psnprintf is a dummy wrapper for vsnprintf, useful for debugging.)
14
5027
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 gives me
0
8392
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,...
0
8912
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8819
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8669
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...
1
6222
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
5692
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();...
0
4222
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4403
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1807
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.