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.
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?
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?