How to stop NetworkStream from buffering TCP packets

48 views Asked by At

I want to send TCP Packets to a server. However I'm having an issue where multiple .Write()s are buffered together to create one bigger packet, despite the MSDN documentation saying that they are not buffered.

The Flush method implements the Stream.Flush method; however, because NetworkStream is not buffered, it has no effect on network streams.

I'm currently sending packets like this:

void TestFunction(NetworkStream stream)
{
  var array = new byte[] { 0x02, 0x00, 0x00, 0x00, 0x8e, 0x00 };
  stream.Write(array);

  array = new byte[] { 0x03, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00 };
  stream.Write(array);

  
  array = new byte[] { 0x0a, 0x00, 0x00, 0x00, 0x24, 0x61, 0x73, 0x64, 0x66, 0x61, 0x73, 0x64, 0x66, 0x00 }:
  stream.Write(array);
}

Investigating the traffic via Wireshark shows me that they're all one big packet though, instead of 3 packets:

char peer1_2[] = { /* Packet 9 */
0x02, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x03, 0x00, 
0x00, 0x00, 0x73, 0x00, 0x00, 0x0a, 0x00, 0x00, 
0x00, 0x74, 0x61, 0x73, 0x64, 0x66, 0x61, 0x73, 
0x64, 0x66, 0x00 };

I did try putting a .Flush() after each .Write(), but that didn't change anything either.

Is there anything I missed? Do I need to use Sockets directly to achieve that functionality? I've quickly looked at their documentation + how NetworkStream's Write is implemented, but couldn't find anything that would point me towards a solution.

0

There are 0 answers