Binarywriter C#

Asked

Viewed 56 times

0

I’m in a little trouble, i use BinaryWriter to write bytes and then send them as a response certainly I use it in the right way(I think).

however, when I use it several times, I break point or capture the answers by Wireshark, looking at the answers he is returning to me (Writing) and it seems that the BinaryWriter is gathering every time I write and then send the reply.

I’ll give you a basic example of the code of 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 send as a response, and sometimes it sends everything separately, but it is difficult to happen.
my problem is this, there is some form of "cut" or "separate" for each answer be individual?

Code is right here:

Pangyabinarywriter

Playersendpackets

Player

  • Also I could not understand very well what you are looking for... but it seems to me to approach the technique of Chunks https://stackoverflow.com/questions/1868279/anyone-have-sample-code-for-doing-a-chunked-http-streaming-downloadof-one-web

  • Okay, thank you very much. I’ll look later

1 answer

0


For me it was a little vague but I will give my solution for what I understood.

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

What you normally do is this:

  • Size [4 bytes]
  • Body of the message [Size]

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

Basically:

  1. read 4 bytes = N
  2. read N bytes
  3. read 4 bytes = N
  4. read N bytes
  5. .........
  • Thanks for the reply, maybe it’s the Binarywriter with memorystream

Browser other questions tagged

You are not signed in. Login or sign up in order to post.