PC Review


Reply
Thread Tools Rate Thread

dynamic buffer size

 
 
davis
Guest
Posts: n/a
 
      3rd Jan 2007
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?

 
Reply With Quote
 
 
 
 
Samuel R. Neff
Guest
Posts: n/a
 
      3rd Jan 2007

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" <(E-Mail Removed)> wrote:

>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?


 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
making dynamic buffer in VB.net to use in C dll vinbelgian Microsoft VB .NET 0 22nd Dec 2006 01:59 PM
buffer size on HDs does size matter? Pdigmking DIY PC 22 6th Dec 2005 02:38 AM
Re: buffer size on HDs does size matter? John Doe Storage Devices 15 6th Dec 2005 02:38 AM
The size of buffer ad Microsoft C# .NET 1 27th Aug 2005 01:47 PM
HD Buffer Size Bob Simon Storage Devices 18 9th Mar 2005 07:34 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:13 PM.