Asynchronous TCP socket bytes merging

后端 未结 2 381
清歌不尽
清歌不尽 2020-12-22 10:20

I wasn\'t quite sure how to explain my problem in the title, but I\'ll try to elaborate on my problem.

Basically I\'m coding a chat that is not P2P, but where all us

2条回答
  •  时光说笑
    2020-12-22 10:52

    This usually happens when two or more packets of data are sent at close intervals. I recently had this problem myself, and the way I resolved it was to a separating key. You can then tokenize each message. For example, you could add the ASCII character #4 (the End-of-Transmission character) to the end of each message being sent like I did.

    Write("Message1" + ((char)4).ToString())
    Write("Message2" + ((char)4).ToString())
    

    Then, when the client receives the data, you can iterate through the received data. When it finds that special character, it knows it's the end of one message, and (maybe) the beginning of a new one.

    "Message1(EOT char)Message2(EOT char)"
    

    \n may be easier to work with than using ASCII characters.

提交回复
热议问题