c# - UDPClient如何响应UDP组播

标签 c# windows-phone-7 udp multicast

首先我想说我对 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# Desktop Client 中的 ConnectionListener:ReceiveCallback

    _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.

关于c# - UDPClient如何响应UDP组播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9652412/

相关文章:

C# HttpListener 部分内容

c# - 如何检查二维数组中的 key 对是否存在?

c# - 为什么禁用按钮会删除其背景颜色?

c# - 文字 Uri 请求

c - 在套接字上设置超时不起作用

node.js - 直播视频必须使用什么协议(protocol)?

c# - 无法构建 C# 项目

c# - 如何在 Windows Phone 7 中检查 3G、wifi、EDGE、蜂窝网络?

windows-phone-7 - 在 Windows Phone 上获取原始麦克风数据?

tcp - iperf TCP 比 UDP 快得多,为什么?