C# streaming sockets, how to separate messages?

后端 未结 6 1132
情歌与酒
情歌与酒 2021-01-15 06:39

Kinda long title, but anyways...

I\'ve been looking at these examples, specifically on the parts on writing and reading the size of the message to the byte streams

6条回答
  •  再見小時候
    2021-01-15 07:25

    Instead of data.Length, try:

    writer.Write(chr(data.Length) + data);
    

    This will prefix every data block with one byte indicating its length (up to 255 bytes long). As you requested, this is only a nudge. :)

    Update: I just remembered that C# is all Unicode and stuff, so chr() probably gives you more than one byte. Adjust to fit.

提交回复
热议问题