如何从UDPClient响应UDP多播

时间:2012-03-11 04:20:24

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

我想在前言中说我对UDP广播和多播的理解非常有限。这是我的第一个项目。

我在计算机和Windows Phone 7应用程序上运行了一个C#桌面客户端。 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应用程序没有收到回复?

2 个答案:

答案 0 :(得分:0)

我认为问题出在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.

答案 1 :(得分:0)

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

相反,您可以使用receiveEndpointEndReceive的{​​{1}}来填充ConnectionListener:ReceiveCallback来向WP7服务器发送unicast响应。建议consideration用于在MSDN创建多播应用程序。

这样,WP7不再需要join组播组用于传入多播,而桌面客户端不需要send组播来响应。