Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The most typical method of doing this involves something along the lines of having the first few bytes of a data chunk be the chunk length.</p> <p>So, for example, if your object size is always under 65k or there abouts, you could send the array size through the socket first as a short (Look at using the BitConverter class). The receiver would read the first chunk it receives into a buffer (probably one roughly the size of a nominal UDP packet, somewhere around 512 bytes), grab the first two bytes, convert them to a short, and set up the buffer for the object. It would then continue to read from the socket until that buffer is full, and pass that buffer off to the de-serializer.</p> <p>This of course doesn't cover error handing and such, but it's the basic idea.</p> <p>EDIT:</p> <p>It's been awhile since I did anything with UDP in .Net, but something like this might work (In somewhat psuedocode)</p> <pre><code>var bigBuffer = new MemoryStream(); //This should be run in a thread void UdpLoop() { while(!done) { var buffer = udpClient.Receive(); bigBuffer.Write(buffer,buffer.Length); } } void MessageProcessor() { var messageLength = 0; while(!done) { if(bigBuffer.Length &gt; 0 &amp;&amp; messageLength == 0) { var lengthBytes = new byte[2]; bigBuffer.Read(lengthBytes, 2); messageLength = BitConverter.ToInt16(lengthBytes); } if(messageLength &gt; 0 &amp;&amp; bigBuffer.Length &gt; messageLength) { var objectBuffer = new byte[messageLength]; bigBuffer.Read(objectBuffer, messageLength); //Do deserialization here } } } </code></pre> <p>That's the basic process, ignoring locking due to multiple threads (which you will have to deal with since Receive bocks), and reading and writing from different spots in the memory stream. Hopefully this is enough to get you going in the right direction.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload