What's the best way to monitor a socket for new data and then process that data?

前端 未结 3 751
忘掉有多难
忘掉有多难 2020-12-10 20:42

Please pardon my C#.Net newbie status. If this is obvious and I missed it from the docs, a link to the relevant page or sample code would be appreciated.

I am workin

3条回答
  •  无人及你
    2020-12-10 20:56

    When you are reading streaming data you need some sentinel/terminating character or a known size of the data to determine when to stop reading and process the information. Null characters or newlines are common. Another technique is using a fixed size header which specifies the length of the body.

    The socket will remain open until you close it or the other side terminates, in which case you will get an error while reading from the socket, which you shuold handle. NetworkStream throws an IOException when the socket has been closed from the remote side during a read or write operation.

提交回复
热议问题