Domanda

I have ported my UDP based protocol implementation from using the Socket.BeginXXX methods to the Socket.XXXAsync methods as they take the SocketAsyncEventArgs class which has Buffer, Offset and Count properties so a single large buffer can be segmented and used for each operation.

Unfortunately I have just found out setting:

mySocketAsyncEventArgs.SendPacketsSendSize

Is not used when sending datagrams using:

mySocket.SendToAsync(mySocketAsyncEventArgs);

Instead the whole segment in SocketAsyncEventArgs.Buffer from Offset for Count bytes is sent! And I cannot use Socket.SendPacketsElements because that is only for connected Sockets!

Often I will only have a few bytes to send, but sometimes a thousand.. If the whole point of these overloads was to increase efficiency - they are not helping!

Please tell me there is way to specify the number of bytes to send in SocketAsyncEventArgs.Buffer on each operation..

È stato utile?

Soluzione 3

You can use the SetBuffer() overload where you do not have to pass a byte[] buffer - just adjust the existing index and count to be used - then after the operation set count back to it's original value:

mySocketAsyncEventArgs.SetBuffer(mySocketAsyncEventArgs.Offset, numOfBytesWritten);
mySocket.SendToAsync(mySocketAsyncEventArgs);
...
mySocketAsyncEventArgs.SetBuffer(mySocketAsyncEventArgs.Offset, originalBufferSize);

Altri suggerimenti

UDP Protocol will always send all data in one packet. you need to use TCP if you want to sent package sized that is not based on data.

Have you tried setting SendPacketsSendSize to 0xFFFFFFFF and using SendPacketsElement.EndOfPacket?. It sounds promising in the docs: http://msdn.microsoft.com/en-us/library/system.net.sockets.socketasynceventargs.sendpacketssendsize.aspx

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top