Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're talking about packets, but that's not a concept exposed by TCP. TCP exposes a stream of bytes, nothing more. It doesn't care how many <code>Send</code> calls there were. It can split one <code>Send</code> call into multiple reads, and merge multiple sends, or a mix of these.</p> <p>The return value of <code>Read</code> tells you how many bytes were read. If this value is larger than 0, but smaller than the length you passed to <code>Read</code>, you got less bytes then you passed it. Your code assumes that either <code>0</code> or <code>length</code> bytes were read. This is an invalid assumption.</p> <p>Your code also suffers from endian issues, but I think both of your systems were little endian, so it's unlikely that this is causing your current problems.</p> <hr> <p>If you don't care about blocking(Your existing code already blocks in the loop, so this is no additional issue) you can simply use a <code>BinaryReader</code> on your stream.</p> <p>It has helper methods like <code>ReadInt32</code> that automatically take care of partial reads, and it uses fixed endianness(always little).</p> <pre><code>buffer = new byte[4]; readBytes = ns.Read(buffer, 0, 4); if (readBytes == 0) return null; int cmdID = BitConverter.ToInt32(buffer, 0); </code></pre> <p>becomes:</p> <pre><code>int cmdId = reader.ReadInt32(); </code></pre> <p>It will throw an <code>EndOfStreamException</code> if it unexpectedly encounters the end of the stream, instead of returning <code>null</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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