【问题标题】:How to respond to UDP Multicast from UDPClient如何从 UDPClient 响应 UDP Multicast
【发布时间】:2012-03-11 04:20:23
【问题描述】:

首先我想说的是,我对 UDP 广播和多播的理解非常有限。这是我的第一个项目。

我有一个在机器上运行的 C# 桌面客户端和一个 Windows phone 7 应用程序。 WP7 应用程序应该通过网络发送 UDP 广播,而桌面客户端应该监听 UDP 多播并做出相应的响应。这只是为了通过网络进行简单的机器发现以查找运行桌面客户端的机器。

C# 桌面客户端代码

    public class ConnectionListener
{
    private const int UDP_PORT = 54322;
    private static readonly IPAddress MULTICAST_GROUP_ADDRESS = IPAddress.Parse("224.0.0.1");

    private UdpClient _listener;

    public ConnectionListener()
    {
        _listener = new UdpClient(UDP_PORT, AddressFamily.InterNetwork);
        _listener.EnableBroadcast = true;
        _listener.JoinMulticastGroup(MULTICAST_GROUP_ADDRESS);

        _listener.BeginReceive(ReceiveCallback, null);
    }

    private void ReceiveCallback(IAsyncResult result)
    {
        IPEndPoint receiveEndpoint = new IPEndPoint(IPAddress.Any, UDP_PORT);
        byte[] receivedBytes = _listener.EndReceive(result, ref receiveEndpoint);

        byte[] response = Encoding.UTF8.GetBytes("WPF Response");
        _listener.BeginSend(response, response.Length, receiveEndpoint, SendCallback, null);
    }

    private void SendCallback(IAsyncResult result)
    {
        int sendCount = _listener.EndSend(result);
        Console.WriteLine("Sent Count is: " + sendCount);
    }
}

WP7 服务器代码:

    public class MachineDetector
{
    public const int UDP_PORT = 54322;
    private const string MULTICAST_GROUP_ADDRESS = "224.0.0.1";

    UdpAnySourceMulticastClient _client = null;
    bool _joined = false;

    private byte[] _receiveBuffer;
    private const int MAX_MESSAGE_SIZE = 512;

    public MachineDetector()
    {
        _client = new UdpAnySourceMulticastClient(IPAddress.Parse(MULTICAST_GROUP_ADDRESS), UDP_PORT);
        _receiveBuffer = new byte[MAX_MESSAGE_SIZE];

        _client.BeginJoinGroup(
            result =>
            {
                _client.EndJoinGroup(result);
                _client.MulticastLoopback = false;
                SendRequest();
            }, null);
    }

    private void SendRequest()
    {
        byte[] requestData = Encoding.UTF8.GetBytes("WP7 Multicast");

        _client.BeginSendToGroup(requestData, 0, requestData.Length,
            result =>
            {
                _client.EndSendToGroup(result);
                Receive();
            }, null);
    }

    private void Receive()
    {
        Array.Clear(_receiveBuffer, 0, _receiveBuffer.Length);
        _client.BeginReceiveFromGroup(_receiveBuffer, 0, _receiveBuffer.Length,
            result =>
            {
                IPEndPoint source;

                _client.EndReceiveFromGroup(result, out source);

                string dataReceived = Encoding.UTF8.GetString(_receiveBuffer, 0, _receiveBuffer.Length);

                string message = String.Format("[{0}]: {1}", source.Address.ToString(), dataReceived);
                Console.WriteLine(message);

                Receive();
            }, null);
    }
}

我可以使用桌面客户​​端接收数据,但 WP7 应用似乎无法接收响应。我一直在努力解决这个问题,不知道还能去哪里看。任何帮助都会很棒。

那么,对于 WP7 应用没有收到响应的任何建议?

【问题讨论】:

    标签: c# windows-phone-7 udp multicast


    【解决方案1】:

    我认为问题在于ConnectionListener:ReceiveCallback 中的C# Desktop Client

        _listener.BeginSend(response, response.Length,
             receiveEndpoint, SendCallback, null);
    

    改为调用

        _listener.BeginSend(response, response.Length,
            SendCallback, null);
    

    这将导致消息被发送到多播地址。如需更多帮助,请参阅How to: Send and Receive Data in a Multicast Group for Windows Phone.

    【讨论】:

    • @AlRodriguez 你的权利。 MSDNBeginSenddestination is to be specified earlier by a call to Connect。但它也说Do not call the Connect method if you intend to receive multicasted datagrams.
    【解决方案2】:

    WP7 需要多播到网络,以便一次有效地访问所有桌面客户端。对于客户端响应,唯一的预期目的地是 WP7。因此,多播在这里没有真正的优势(因为调到多播的桌面客户端实际上会忽略它)。

    相反,您可以使用在ConnectionListener:ReceiveCallback 中调用EndReceive 填充的receiveEndpoint 向WP7 服务器发送unicast 响应。建议 considerationMSDN 创建多播应用程序。

    这样,WP7 不再需要join 组播组来接收多播,桌面客户端也不需要send 多播来响应。

    【讨论】:

      猜你喜欢
      • 2013-04-19
      • 1970-01-01
      • 2010-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-06
      • 2016-04-30
      • 1970-01-01
      相关资源
      最近更新 更多