正确使用BeginReceive/EndReceive? [英] Correct use of BeginReceive / EndReceive?

查看:117
本文介绍了正确使用BeginReceive/EndReceive?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要从套接字异步接收数据,.Net支持对称的BeginReceive/EndReceive调用.基本上,您调用 BeginReceive()来开始侦听并标识在数据到达时应调用的回调.在回调内部,您调用 EndReceive()提取数据并结束异步读取操作.

To asynchronously receive data from a socket .Net supports symmetrical BeginReceive/EndReceive calls. Basically you call BeginReceive() to start listening and to identify a callback which should be called when the data arrives. Inside the callback you call EndReceive() to extract the data and end the asynchronous read operation.

我正在编写C#/.Net软件来控制某些工业设备.控制面板允许用户设置和初始化设备,一旦初始化, BeginReceive()就被称为开始监听数据.在回调中, EndReceive()用于提取数据,但是我想立即恢复监听,所以我认为在执行 EndReceive().这是正确的吗?

I'm writing C#/.Net software to control some industrial equipment. A control panel allows users to set up and initialize the equipment and once it's initialized BeginReceive() is called start listening for data. In the callback EndReceive() is used to extract the data, but I want to resume listening right away so I think I should call BeginReceive() again right after doing the EndReceive(). Is this correct?

如果是这样,是否可以在代码中的其他地方使用检查或测试来了解是否已经调用了 BeginReceive(),所以我不要尝试调用 BeginReceive()在调用 EndReceive()之前,在同一套接字上连续两次?

If so, is there a check or test I can use elsewhere in the code to know whether BeginReceive() has already been called so I don't try to call BeginReceive() twice in a row on the same socket, before EndReceive() has been called?

推荐答案

避免在调用 EndReceive()之前再次调用 BeginReceive()的方法是:将对 BeginReceive()的调用放入完成回调中,在其中您调用 EndReceive().当然,唯一不应该执行的对 BeginReceive()的调用是您在 Socket 连接后立即进行的调用.

The way you avoid calling BeginReceive() again before you've called EndReceive() is to put the call to BeginReceive() into the completion callback where you call EndReceive(). The only call to BeginReceive() that should not be executed there is, of course, the one you make immediately after the Socket has connected.

请注意:在接收完成之前,可以多次调用 BeginReceive().但是,当您这样做时,需要确保进行必要的整理工作,以确保以正确的顺序处理数据(即,以与通过 BeginReceive()提交缓冲区的顺序相同的顺序处理缓冲区.代码>).

To be clear: it is permitted to call BeginReceive() any number of times before any receive completion happens. But when you do that, you need to make sure you do the necessary housekeeping to ensure you process the data in the right order (i.e. you process the buffers in the same order in which you submitted them via BeginReceive()).

所以上面的答案是关于不必做所有的内务处理,从而使代码更简单.

So the above answer is about not having to do all that housekeeping and thus keeping the code simpler.

如果您仅在调用 EndReceive()的同一位置对 BeginReceive()进行后续调用,则使事情井然有序很简单.请注意,尽管如此,您仍然需要正确执行操作:确保始终以正确的顺序接收缓冲区的最简单方法是,直到之后 BeginReceive()>您已调用 EndReceive()(但仍使用相同的方法).

If you only ever make your subsequent calls to BeginReceive() in the same place where you are calling EndReceive(), it's trivial to keep things in order. Note though that you still need to do that right: the simplest way to ensure you are always receiving the buffers in the right order is to simply not call BeginReceive() again until after you've called EndReceive() (but still in the same method).

这篇关于正确使用BeginReceive/EndReceive?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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