UDP - Can I send two datagram parts, and make the receiving end combine them into one?

拜拜、爱过 提交于 2019-12-10 05:28:47

问题


This is maybe a stupid question, but since I am relatively new to UDP here it goes... If I am having two separate byte arrays that I need the receiving side to get as one big array, for example:

byte[] Array1 = {1,1,1}
byte[] Array2 = {2,2,2}

Can I avoid having to create a buffer and copy each array into it, and then send that buffer, like this:

byte[] Buffer= new byte[Array1.Length + Array2.Length];
Buffer.BlockCopy(Array1, 0, Buffer, 0, Array1.Length);
Buffer.BlockCopy(Array2, 0, Buffer, Array1.Length, Array2.Length);

udpClient.Send(Buffer, Buffer.Length);

Because if the two are big, and data rate is high, copying uses up much system resources... So can I somehow tell the udpClient that I am starting the UDP fragmentation, and then do like this:

udpClient.ImStartingOneBigDatagram();

udpClient.Send(Array1, Array1.Length);
udpClient.Send(Array2, Array2.Length);

udpClient.ThatsAllFolks();

And be sure that the receiving side would get:

byte[] recv = {1,1,1,2,2,2}

I am using C# for this, and I dont need to use UdpClient, I was just making my point.


回答1:


Use the equivalent of Win32 API's WSASendMsg:

public int Send(
    IList<ArraySegment<byte>> buffers,
    SocketFlags socketFlags,
    out SocketError errorCode
)

http://msdn.microsoft.com/en-us/library/ms145161.aspx

But ultimately this is premature optimisation, if you perform some testing you will see that to use I/O scatter gather arrays you need at least 9KB of data to get performance improvement. For small buffers, i.e. less than a page size (4KB on x86) it is faster to build a contiguous buffer yourself before passing to the socket API.




回答2:


Yes, you can split any data block into two parts and send it out as two separate UDP packets. However, UDP does not guarantee delivery, so one of the pieces may go missing in transit. What will you do if the recipient receives only half the message? Throw it away, obviously, but something to keep in mind with UDP.

So, to answer the last part of your question "And be sure that the receiving side would get..." the answer is no. You're using UDP, so you can't be sure the receiving side would get anything.

If you are wanting to break up the send into multiple chunks for your convenience rather than for network packet size concerns, you can certainly write your own wrapper class that sits above the UDPclient and accepts multiple chunks of data via a Send method call and only sends all the data to the udpclient.Send() when you call .ThatsAllFolks on your wrapper class. But that's not going to reduce memory overhead since the wrapper has to accumulate the data in memory before the Big Send. This wrapper would just be a cosmetic convenience for your client code.



来源:https://stackoverflow.com/questions/6538657/udp-can-i-send-two-datagram-parts-and-make-the-receiving-end-combine-them-int

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!