Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send a message and receive a response on the same socket

Tags:

c#

sockets

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.

like image 339
Gavin Avatar asked Jan 18 '23 09:01

Gavin


1 Answers

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();
}
like image 90
Aoi Karasu Avatar answered Feb 03 '23 21:02

Aoi Karasu