NetworkStream.Read - 它可以收集部分消息吗?
我正在创建一个客户端服务器应用程序,我只是想知道一件事。
客户端将Query对象序列化为String,然后使用NetworkStream将其发送到服务器。
服务器使用 GetStream().Read 读取字节,将其写入缓冲区,将该字符串反序列化为 Query 对象,并获取存储在接收到的查询中的信息。
Read() 方法是否有可能返回部分消息(尚未完全到达)? 或者,例如,在立即发送两条消息的情况下,是否可以通过一次调用 Read() 方法将两条消息读取为一串字节?
或者,也许 NetworkStream 以某种方式知道如何识别完整的消息,并且每个 Read() 都与每个消息相关联?
先感谢您。
I'm creating a client-server application and I'm just wondering about one thing.
Client serializes Query object to String and then uses NetworkStream to send it to the server.
Server reads bytes using GetStream().Read, writes it into a buffer, deserializes that string into a Query object and gets information stored in received query.
Is it possible that Read() method could return partial message (that has not already arrived completely)?
Or, for example, in case of immediate sending two messages, is it possible to read two messages as one string of bytes, by one call of Read() method?
Or maybe NetworkStream somehow knows how to recognize complete messages and each Read() is associated with each message?
Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,
如果您看到 MSDN 文档它说:
因此,请务必检查
Read()
方法的返回 (int) 值,并查看该方法是否已读取预期字节数。很可能没有。
Yes,
if you see MSDN Doc it says:
So Always check the returned (int) value of the
Read()
method and see if the method has read Expected number of Bytes.Most Probably No.