Determine if there is Data left on the socket and discard it

后端 未结 2 906
被撕碎了的回忆
被撕碎了的回忆 2020-12-04 03:58

I\'m writing an Interface under Linux which gets Data from a TCP socket. The user provides a Buffer in which the received Data is stored. If the provided Buffer is to small

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-04 04:21

    There is no knowledge at TCP level about what constitutes an application protocol message. There are, however, two most common ways to delimit messages in a TCP stream:

    • prefix messages with their size and read that many bytes, or
    • read till a certain sequence of bytes is found.

    In this light, a generic TCP reader should provide two reading functions to be universally useful:

    • to read N bytes, and
    • to read till a delimiter has been read

    A design similar to Tornado IOStream reading functions would do.

提交回复
热议问题