BinaryWriter C #

0

I'm having a little problem, I use BinaryWriter to write the bytes and then be sent as a response surely I use it right (I think).

However, when I use it several times, I give breakpoint or capture the answers by WireShark , looking at the answers it returns me (Writing) and it seems like BinaryWriter is joining every time I write and then I send the answer.

I'll give you a basic example of the code the way I use it:

Response.Write(new byte[] { 0x0F, 0x00, 0x00 }); //chamada da classe é Response

Response.WritePStr(UserInfo.Username); //Escreve String e converte em bytes

SendResponse(); //copia os bytes escritos da PangyaBinaryWriter, usando o Response

Response.Write(new byte[] { 0x01, 0x00, 0xD9, 0xFF, 0xFF, 0xFF, 0xFF });

SendResponse(); 

If I leave it this way it "seems" to put everything together and sends it back, and sometimes it sends everything apart, but it's hard to do that. my problem is this, is there any way to "cut" or "separate" for each individual answer?

Code is here:

PangyaBinaryWriter

PlayerSendPackets

Player

    
asked by anonymous 12.11.2018 / 14:22

1 answer

0

For me it got a bit vague but I will give my solution as I understood.

This behavior is normal, packages are not differentiated the way you want just because you want. You must differentiate them yourself. Normally when you send two packets like this, the two arrive "together" and the connection provider does not know that there are two packets.

What you do is usually the following:

  • Size [4 bytes]
  • Message Body [Size]

The body of the message is defined by the SIZE, so you will always know how much to read after receiving the first 4 bytes. Soon after reading the first 4 + the body, the next 4 bytes will refer to the next size of the package and so on.

Basically:

  • read 4 bytes = N
  • read N bytes
  • read 4 bytes = N
  • read N bytes
  • .........
  • 12.11.2018 / 14:32