相当于 C# 异步套接字中的 Peek?
我习惯使用同步套接字。为了处理尚未完全到达的消息,我将前 4 个字节设置为消息的预期长度。然后,我将使用 Socket.Receive(tcpRecv, 1024, SocketFlags.Peek); 来查看消息,而不将其从缓冲区中取出。如果所有这些都在那里,我就会提取数据。如果不是,我会把它留在那里。我设计的协议确保消息不会超过 1024 字节。
在异步套接字中,我看不到查看数据的方法。有办法做到这一点吗?有没有比查看数据更好的方法?
谢谢。
-缺口
I'm used to using synchronous sockets. In order to deal with messages that have not completely arrived yet, I'd set the first 4 bytes to be the expected length of the message. Then I'd use Socket.Receive(tcpRecv, 1024, SocketFlags.Peek);
to take a look at the message without pulling it off the buffer. If all of it was there, I'd pull the data. If it wasn't, I'd leave it there. I had designed my protocol so that no message would ever be greater than 1024 bytes.
In asynchronous sockets, I don't see a way to peek at the data. Is there a way to do this? Is there a better approach to this than peeking at the data?
Thanks.
-Nick
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无需查看:.NET 异步套接字允许您无需查看即可实现相同类型的功能。我想您可能正在寻找这样的东西:
当您收到回调时,您可以安排另一个接收:
您可以在我的博客上看到整个异步客户端:http://codesprout.blogspot.com/2011/04/asynchronous-http-client.html
You don't need to peek: .NET asynchronous sockets allow you to achieve the same type of functionality without peeking. I think you might be looking for something like this:
When you get the callback you can schedule another receive:
You can see the entire asynchronous client on my blog: http://codesprout.blogspot.com/2011/04/asynchronous-http-client.html
您的相同数据流无需偷看即可工作:
与偷看的唯一区别是您必须在最初读取它们时保存四个字节。
Your same data flow works without peeking:
The only difference from peeking is that you have to save the four bytes when you initially read them.