I have a C# .Net 3.5 application that sends a multicast “Hello” packet to whomever may be subscribed to a particular multicast group and then listens for all the responses. So, every X seconds, I may send a “Hello” packet and make a note of everybody who responds.
It is intended to be used like this:
MulticastHello hello_ = new MulticastHello();
// alert our UI of any responses to the 'Hello'
hello_.ReceivedHelloResponse += OnHelloResponse;
// this timer function is triggered every X seconds
private void OnTimer(object sender, EventArgs e)
{
// stop listening for responses to the last 'hello'
hello_.CancelHello();
// send a new 'hello' and start listening for responses
hello_.SendHello("224.0.100.1");
}
Unfortunately, I’m having issues canceling the asynchronous read. My private void OnReceive(IAsyncResult ar) function will occasionally throw a System.ArgumentException that says “The IAsyncResult object was not returned from the corresponding asynchronous method on this class.”
How can I reliably cancel an asynchronous socket operation. Or, is there a better way of doing this?
My implementation is below.
Thanks,
PaulH
public class HelloResponseEventArgs : EventArgs { /*...*/ }
public class MulticastHello : IDisposable
{
public event EventHandler<HelloResponseEventArgs> ReceivedHelloResponse;
private Socket socket_;
private byte[] received_ = new byte[HelloResponse.Size];
private EndPoint responder_ = new IPEndPoint(IPAddress.Any, 0);
protected virtual void OnReceivedHelloResponse(HelloResponseEventArgs e)
{
EventHandler<HelloResponseEventArgs> evt = ReceivedHelloResponse;
if (null != evt)
evt(this, e);
}
private void OnReceive(IAsyncResult ar)
{
IPEndPoint ipendpoint = new IPEndPoint(IPAddress.Any, 0);
EndPoint endpoint = ipendpoint as EndPoint;
try
{
socket_.EndReceiveFrom(ar, ref endpoint);
}
catch (System.ObjectDisposedException)
{
// the read was canceled. This is expected.
return;
}
// decode the response and set the event
IPEndPoint remote = endpoint as IPEndPoint;
HelloResponse response = new HelloResponse(Deserialize<HelloPacket>(received_));
OnReceivedHelloResponse(new HelloResponseEventArgs(remote.Address.ToString(), response));
// keep receiving responses until canceled
socket_.BeginReceiveFrom(received_,
0,
received_.Length,
SocketFlags.None,
ref endpoint,
new AsyncCallback(OnReceive),
null);
}
// stop listening for responses to the hello frame
public void CancelHello()
{
if (socket_ != null)
socket_.Close();
}
// send an initial 'Hello' to the a multicast address. Start listening for responses
public void SendHello(string address)
{
socket_ = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socket_.MulticastLoopback = false;
socket_.Ttl = 255;
HelloResponse send = new HelloResponse();
byte[] data = Serialize(send.Packet);
EndPoint remote = new IPEndPoint(IPAddress.Parse(address), 7);
socket_.SendTo(data, remote);
socket_.BeginReceiveFrom(received_,
0,
received_.Length,
SocketFlags.None,
ref responder_,
new AsyncCallback(OnReceive),
null);
}
#region IDisposable Members
/* close the socket on dispose*/
#endregion
}
Close()doesn’t wait. It exits immediately. So, if the next SendHello() comes before EndReceiveFrom() finishes it will throw theSystem.ArgumentException.The solution is to wait on an event object after the
Close()call that is set when theSystem.ObjectDisposedExceptionis caught inOnReceive.-PaulH