How to Respond to UDP Multicast from UDPClient

I want to preface this by saying that my understanding of UDP Broadcasting and Multicast is very limited. This is my first project to work on this.

I have a C # desktop client running on a computer and a Windows Phone 7 application. The WP7 application is supposed to send a UDP broadcast over the network and the desktop client should listen for UDP multicast and respond accordingly. It is simply intended to do simple machine discovery over the network to find machines running the desktop client.

C # desktop code

    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 Server Code:

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

      

I can get data with the desktop client, but the WP7 app can't seem to get a response. I've been knocking on this head for several seconds now and I don't know where else to look. Any help would be great.

So any suggestions why the WP7 app isn't getting a response?

+3


source to share


2 answers


I think the problem is related to ConnectionListener:ReceiveCallback

the C# Desktop Client

.

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

      

Instead, call



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

      

This will send a message to the multicast address. For more help, see How to Send and Receive Data in a Multicast Group for Windows Phone.

0


source


WP7 is required for network multicast to effectively reach all desktop clients in a single sweep. For customer response, WP7 is the only assigned destination. Thus, multicast has no real advantage here (since desktop clients configured in multicast will effectively ignore it).

Instead, you can use the receiveEndpoint

filled in call EndReceive

to ConnectionListener:ReceiveCallback

to send a response unicast

to the WP7 server. consideration

MSDN is recommended for creating multicast applications .



Thus, WP7 no longer needs a join

multicast group for inbound multicast, and the desktop client does not need a send

multicast to respond.

0


source







All Articles