Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3948256
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:21:10+00:00 2026-05-20T01:21:10+00:00

I have a C# .Net 3.5 application that sends a multicast Hello packet to

  • 0

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
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T01:21:11+00:00Added an answer on May 20, 2026 at 1:21 am

    Close() doesn’t wait. It exits immediately. So, if the next SendHello() comes before EndReceiveFrom() finishes it will throw the System.ArgumentException.

    The solution is to wait on an event object after the Close() call that is set when the System.ObjectDisposedException is caught in OnReceive.

    -PaulH

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .NET application that is meant to be run on a local
I have a .NET application that processes around 300,000 records in a batch import,
I have a ASP.NET application that we've written our own logging module for. My
I have an ASP.net Application that runs on the internal network (well, actually it's
We have an ASP.NET application that manages it's own User, Roles and Permission database
I have an .Net Froms application that displays web pages through a WebBrowser control.
I have an ASP .NET application that connects to an Oracle or a SQL
I have a .NET client application that needs to communicate with a server using
I have a small VB.NET application that I'm working on using the full version
I have a VB.net test application that clicks a link that opens the Microsoft

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.