通过套接字异步数据接收 [英] Asynchronously data receivement via socket

查看:93
本文介绍了通过套接字异步数据接收的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我正在编写一个代码,我将通过套接字调用BeginReceive方法接收数据,这是异步的。我需要知道何时完成接收数据包,因为我将测量发送和接收数据之间的时间。我该怎么办
呢?我在MSDN上寻找解决方案而且我找到了"IsCompleted"属性,但是它对我来说是不对的。 

I'm writing a code where I'll receive data via socket calling the method BeginReceive, that is asynchronous. I need to know when I finished to receive a data package, cause I'll measure the time between the sending and receiving of the data. How can I do this? I was looking for solutions on MSDN and I found the "IsCompleted" property, but it does not work the right way for me. 

推荐答案

无法保证当您发送数据包时,数据字节将到达同一批次。顺便说一下,Socket甚至TCP都不关心你的数据边界在哪里(例如,你在2个Send()调用中发送2个数据包,接收
结束可能会调用3个BeginReceive,第二个读取包含从第一个结尾和第二个包开始的数据都是这样的,因此建议您将所有收到的数据存储在缓冲区中并自行检查分隔符。
There's no guarantee that when you send a package of data out, the bytes of data will arrive in the same lot. And by the way, Socket or even TCP does not care where your data boundary are (say, you send 2 data packages in 2 Send() calls, the receiving end may invoke 3 BeginReceive, with the second read containing both data from the end of first and the begining of second package) , so you're advised to store any data received in a buffer and check for delimiter yourself.


这篇关于通过套接字异步数据接收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆