如何在同一个套接字上发送消息并接收响应

时间:2012-02-23 15:40:30

标签: c# sockets

我有以下代码发送多播消息,然后等待响应发送到消息来自的地址。如果我在Wireshark中观察流量,我可以看到消息发送正常并且响应返回到正确的IP和端口,但是套接字永远不会从接收线返回,就像响应没有被接收一样。

    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();
    }

我知道我应该在socket类上使用异步方法,并且需要停止依赖Thread.Sleep,但我只想在整理代码之前得到一个简单的示例。

1 个答案:

答案 0 :(得分:9)

加文,看看这个:

  • 请勿使用其他端口。你期望如何在一个上多播并在另一个上接收?
  • 不要使用Connect(),多播是无连接消息(就像广播一样)。
  • Bind()之后将套接字选项设置为多播。
  • 使用SendTo()代替Send(),在这种情况下不起作用。
  • 首先开始接收(即使在阻止模式下,它是一个不同的端点),然后发送。

一个简单的工作示例:

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();
}