receiving large udp dgrams / how to avoid buffer overflow?

C

Christoph Bisping

Hello!

I'm not sure if this is the right place to ask my question...
According to the docs, it is possible to receive udp datagrams asynchronous
using the Socket-class and its .BeginRecive(From) method. My problem is that
I can't figure out how big the receive-buffer should be.

When my programs fires the receive-callback function, I call the
EndReceive(From) method to copy the received datagrams to the buffer that
I've supplied earlier in the BeginReceive(From) method. It's a
Byte()-variable with a fixed size (32kb).

-->
m_Gateway.BeginReceiveFrom(m_Buffer, 0, m_Buffer.Length, SocketFlags.None,
m_remoteEP, AddressOf UDPCallback, Nothing)

<--

m_Buffer is initialized with a fixed size of 32kb.

Whenever my application receives a large amount of data (>32kb) my
receive-callback terminates with an exception "there was more data to
receive" (translated from german)... Of course, this error only occures when
my receive-buffer is too small:

-->

lRead = m_Gateway.EndReceiveFrom(iarg, m_remoteEP)

<--


Is there any possibility to "see" the size of the received data -before-
calling .EndReceive(From)? I've tried the .Available property but its value
is either "0" or much too small.

It can't be that I need to supply a hardcoded-sized Byte()-buffer, can it?


Greetings,
Christoph Bisping
 
E

Ed Kaim [MSFT]

The data is put directly into your buffer, so there really isn't a way to
receive a datagram bigger than the buffer you supply. Are you responsible
for the sending side? If so, one soltuion would be to chunk the data into
more packets that don't exceed the size. Otherwise, I think the UDP size
field is only 16 bits, so passing a 64K buffer should always work because
the packet can't exceed 65535 bytes (including header and any options).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top