Increment socket receive buffer pointer?

O

O.B.

In C# 2.0, I am processing data from a socket. There are cases, where
I pull 12 bytes from the socket and determine that the next X bytes
should be ignored, where X is determined by observing each 12 bytes of
data.

Currently, I am allocating a byte array of X bytes and using
Socket.Receive(...) to pull the remaining X bytes off the socket and
discarding them:

byte[] discardData = new byte[numBytesToDiscard];
socket.Receive(discardData, 0, numBytesToDiscard, SocketFlags.None);

Is there a way in C# to just push the socket pointer up by X bytes
without having to copy the bytes into an array?
 
W

William Stacey [C# MVP]

No. That data will be in the internal buffers and needs to be read. From a
network perspective you should always read as much as you can each read,
then process that chunk as you need instead many small reads.

--
William Stacey [C# MVP]

| In C# 2.0, I am processing data from a socket. There are cases, where
| I pull 12 bytes from the socket and determine that the next X bytes
| should be ignored, where X is determined by observing each 12 bytes of
| data.
|
| Currently, I am allocating a byte array of X bytes and using
| Socket.Receive(...) to pull the remaining X bytes off the socket and
| discarding them:
|
| byte[] discardData = new byte[numBytesToDiscard];
| socket.Receive(discardData, 0, numBytesToDiscard, SocketFlags.None);
|
| Is there a way in C# to just push the socket pointer up by X bytes
| without having to copy the bytes into an array?
|
 
B

Barry Kelly

O.B. said:
byte[] discardData = new byte[numBytesToDiscard];
socket.Receive(discardData, 0, numBytesToDiscard, SocketFlags.None);

Don't forget that you *must* look at the return value of Socket.Receive
to find out how many bytes were actually read. It is not an error for it
to be less than the number you looked for, but if it is zero, then it's
the other end is closed etc.
Is there a way in C# to just push the socket pointer up by X bytes
without having to copy the bytes into an array?

It's better to implement your own buffering solution, for better
performance.

-- Barry
 

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