Problem with Socket.EndReceive Returning Invalid Value

O

O.B.

I have a C# Socket configured for streaming TCP. Upon making a valid
connection, I have an asynchronous callback configured using
BeginReceive. Within the BeginReceive declaration, I set the socket
flags to SocketFlags.Partial to allow my callback to be invoked as
soon as it arrives rather than waiting for the receive buffer to fill
up.

The problem I am having is that when I invoke the
Socket.EndReceive(asyncResult) operation within the callback, it
sometimes returns a value less than what is stored in the receive
buffer. For example, the receive buffer of 2048 bytes has 76 bytes of
real data in the buffer. However, the Socket.EndReceive operation
returned 74 when it was invoked.

Is this a known bug with .NET 2.0? Or am I doing something wrong
syntactically?
 
P

Peter Duniho

O.B. said:
I have a C# Socket configured for streaming TCP. Upon making a valid
connection, I have an asynchronous callback configured using
BeginReceive. Within the BeginReceive declaration, I set the socket
flags to SocketFlags.Partial to allow my callback to be invoked as
soon as it arrives rather than waiting for the receive buffer to fill
up.

The problem I am having is that when I invoke the
Socket.EndReceive(asyncResult) operation within the callback, it
sometimes returns a value less than what is stored in the receive
buffer. For example, the receive buffer of 2048 bytes has 76 bytes of
real data in the buffer. However, the Socket.EndReceive operation
returned 74 when it was invoked.

Is this a known bug with .NET 2.0? Or am I doing something wrong
syntactically?

Without seeing the code, it's hard to say. However, some suggestions:

* As long as the next call to Begin/EndReceive returns the correct
subsequent bytes, I wouldn't not worry too much if it _looks_ like you
have valid data beyond that. It might just be coincidence, or data left
from a previous receive (depending on how you manage the buffer).

* I don't understand why you are setting SocketFlags.Partial.
That's for message-oriented communications, and should be entirely
irrelevant for a TCP socket. With or without the flag, TCP will return
some data as soon as it can, whether or not there was enough to fill the
buffer you've provided.

Pete
 
O

O.B.

I have a C# Socket configured for streaming TCP. Upon making a valid
connection, I have an asynchronous callback configured using
BeginReceive. Within the BeginReceive declaration, I set the socket
flags to SocketFlags.Partial to allow my callback to be invoked as
soon as it arrives rather than waiting for the receive buffer to fill
up.

The problem I am having is that when I invoke the
Socket.EndReceive(asyncResult) operation within the callback, it
sometimes returns a value less than what is stored in the receive
buffer. For example, the receive buffer of 2048 bytes has 76 bytes of
real data in the buffer. However, the Socket.EndReceive operation
returned 74 when it was invoked.

Is this a known bug with .NET 2.0? Or am I doing something wrong
syntactically?


Dang. I hate it when this happens ... I was doing something wrong.
EndReceive() is correct; my code was referencing a bad buffer.
 

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