.NET Socket.BeginReceive:何时触发回调 [英] .NET Socket.BeginReceive: When is callback triggered

查看:43
本文介绍了.NET Socket.BeginReceive:何时触发回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直想知道BeginRecieve调用所提供的回调何时触发.

I've been wondering when the call back that's provided with a BeginRecieve call is triggered.

  • 当它接收到缓冲区可以容纳的尽可能多的数据时,是吗?如果是的话-如果数据小于缓冲区怎么办?
  • 它收到一个TCP/IP数据包时是吗?
  • 还有别的吗?

我发现了一个类似的问题,由于我无法说得更清楚,我将重复该问题:

I found a similair question which I will repeat as I can't put it much clearer:

现在所有文档都说该回调,如BeginReceive,在接收到数据"后立即调用.但这是相当模糊:如果您不知道那一刻到底是什么时候到底是其他进程在提供数据?

Now all documentation says that the callback, as specified in BeginReceive, is called as soon as 'the data is received'. But this is rather vague: when exactly is that moment if you don't know how exactly that other process is providing data?

一个标准是BeginReceive()被认为已完成(因此当状态对象中的缓冲区已满时,将调用callbask)直到指定的缓冲区大小.但是,如果交付"过程是是否以未知数量和不规则模式提供数据?为了例如,如果它先连续传送100个字节,然后是1毫秒的时间间隔,其后是200字节:BeginReceive是否完整包含100字节的传入数据?还是300?

One criterium is that BeginReceive() is considered completed (and thus the callbask is called) when the buffer in the state object is filled upto the specified buffersize. But what if the 'delivering' process is feeding data in unknown quantities and in an irregular pattern? For instance, if it first delivers 100 bytes consecutively, and then there is a time interval of 1 millisecond and another 200 bytes follow: does BeginReceive complete with 100 bytes of incoming data? Or 300?

http://www.pcreview.co.uk/forums/exactly-beginreceive-socket-considered-completed-t2899270.html

推荐答案

我的经验是,要求任何可用的数据都可以无延迟地调用它.这意味着读取可能很小,例如1400字节左右,因为这大约是MTU大小.

My experience is that it is called for any data being available without delay. This means that reads can be rather small, like 1400 bytes or so because that is on the order of the MTU size.

读取次数可能是该次数的倍数,因为如果数据包到达顺序不正确,则在逻辑上第一个到达时,所有数据包都会对应用程序可见.在这种情况下,您可以一次读取所有连续排队的数据包.

Reads can be multiples of that because if packets arrive out of order all of them are made visible to application the moment the logically first one arrives. You get to read from all contiguous queued packets at once in this case.

我猜您的读取大小将在极快的连接上增加,因为您的应用程序可能无法像网络传递单个数据包一样快地使字节出队.

I guess your read size will increase on extremely fast connections because your application might fail to dequeue bytes as fast as the network delivers individual packets.

旁注:BeginReceive-callback在Receive刚返回的同一时刻被调用.您不会以这种方式减少延迟.(由于异步操作的开销可能比阻塞操作的开销大,因此延迟实际上会增加很小的一部分.)

Sidenote: The BeginReceive-callback is called at exactly the same moment Receive would have returned. You don't get to reduce latency that way. (Latency will actually increase by a tiny amount because async operations can have higher overhead than blocking ones).

这篇关于.NET Socket.BeginReceive:何时触发回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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