3

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

I have a C# desktop client running on a machine and a Windows phone 7 app. The WP7 app is supposed to send a UDP broadcast over the network and the desktop client is supposed to listen for a UDP Multicast and respond accordingly. This is just meant for simple machine discovery over the network to find machines running the desktop client.

C# Desktop Client 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);
    }
}

The 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 am able to receive data with the desktop client, but the WP7 app doesn't seem able to receive the response. I've been banging my head on this for a while now and don't know where else to look. Any help would be great.

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

ProgrammerAl
  • 748
  • 8
  • 20

2 Answers2

0

I think the issue is with ConnectionListener:ReceiveCallback in C# Desktop Client.

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

Instead call

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

This will cause the message to be sent to the multicast address. For further help with this refer to How to: Send and Receive Data in a Multicast Group for Windows Phone.

user1055604
  • 1,624
  • 11
  • 28
  • @AlRodriguez Your right. [MSDN](http://msdn.microsoft.com/en-us/library/dx13cwba.aspx) says about `BeginSend` that `destination is to be specified earlier by a call to Connect`. But it also says `Do not call the Connect method if you intend to receive multicasted datagrams.` – user1055604 Mar 12 '12 at 17:51
0

WP7 needs to multicast to the network in order to efficiently reach all desktop clients in one sweep. For the client response, the only intended destination is WP7. As such, a multicast has no real advantage here (as the desktop clients tuned into the multicast will effectively be ignoring it).

Instead, you could use receiveEndpoint populated by the call to EndReceive in ConnectionListener:ReceiveCallback to send a unicast response to the WP7 server. This is recommended consideration for creating multicast applications at MSDN.

This way WP7 no longer needs to join the multicast group for incoming multicast and the desktop client needn't send a multicast to respond.

Community
  • 1
  • 1
user1055604
  • 1,624
  • 11
  • 28