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 7564313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:52:24+00:00 2026-05-30T13:52:24+00:00

I have the following code that sends a multicast message then waits for a

  • 0

I have the following code that sends a multicast message then waits for a response to be sent to the address the message came from. If I watch the traffic in Wireshark I can see the message sends ok and a response comes back to the correct IP and port however the socket never returns from the receive line, it’s like the response is not being picked up.

    var multicastAddress = IPAddress.Parse("239.255.255.250");
    var multicastPort = 1900;
    var unicastPort = 1901;        

    using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
    {
        socket.Bind(new IPEndPoint(IPAddress.Any, unicastPort));
        socket.Connect(new IPEndPoint(multicastAddress, multicastPort));
        var thd = new Thread(() =>
             {
                 try
                 {
                     while (true)
                     {
                         var response = new byte[8000];
                         EndPoint ep = new IPEndPoint(IPAddress.Any, unicastPort);
                         socket.ReceiveFrom(response, ref ep);
                         var str = Encoding.UTF8.GetString(response);
                         Devices.Add(new SsdpDevice() {Location = str});
                     }
                 }
                 catch
                 {
                     //TODO handle exception for when connection closes
                 }
             });
        socket.Send(broadcastMessage, 0, broadcastMessage.Length, SocketFlags.None);
        thd.Start();
        Thread.Sleep(30000);
        socket.Close();
    }

I know I should be using the asynchronous methods on the socket class and need stop relying on Thread.Sleep but I just want to get a simple example working before I tidy up the code.

  • 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-30T13:52:26+00:00Added an answer on May 30, 2026 at 1:52 pm

    Gavin, check this out:

    • Don’t use different ports. How do you expect to multicast on one and receive on another?
    • Don’t use Connect(), multicast is connectionless messaging (just as broadcast is).
    • Set socket option to multicast after Bind().
    • Use SendTo() instead of Send(), which won’t work in this case.
    • First start receiving (even in blocking mode, it’s a different endpoint), then send.

    And a simple working example:

    var broadcastMessage = Encoding.UTF8.GetBytes("Hello multicast!");
    var multicastAddress = IPAddress.Parse("239.255.255.250");
    var signal = new ManualResetEvent(false);
    var multicastPort = 1900;
    
    using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
    {
        var multicastEp = new IPEndPoint(multicastAddress, multicastPort);
        EndPoint localEp = new IPEndPoint(IPAddress.Any, multicastPort);
    
        // Might want to set this:
        //socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1); 
        socket.Bind(localEp);
        socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(multicastAddress, IPAddress.Any));
        // May want to set this:
        //socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 0); // only LAN
        var thd = new Thread(() =>
            {
                var response = new byte[8000];
                socket.ReceiveFrom(response, ref localEp);
                var str = Encoding.UTF8.GetString(response).TrimEnd('\0');
                Console.WriteLine("[RECV] {0}", str);
                signal.Set();
                Console.WriteLine("Receiver terminating...");
            });
        signal.Reset();
        thd.Start();
    
        socket.SendTo(broadcastMessage, 0, broadcastMessage.Length, SocketFlags.None, multicastEp);
        Console.WriteLine("[SEND] {0}", Encoding.UTF8.GetString(broadcastMessage));
        signal.WaitOne();
        Console.WriteLine("Multicaster terminating...");
        socket.Close();
        Console.WriteLine("Press any key.");
        Console.ReadKey();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code that sends a message using the mail() function. Everything
I have the following code that loads a user's Active Directory DirectoryEntry object from
I have a rails code that sends emails. Following is in my controller: def
I have the following code (thanks to input from 'jon Z' and 'manojlds') that
I have an ASP.NET program that sends a confirmation email with the following code:
I have the following code to read messages sent from a server to the
I have the following code for an echo client that sends data to an
I have the following code that requests the Google.com homepage and sends the page
The following code waits for data over UDP. I have a test function that
I have the following code that works on PHP5 to send a HTTP POST

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.