Search code examples
c#network-programmingudpbroadcasting

UDP broadcasting system won't communicate on separate computers


I am working on a broadcast beacon in C# that is supposed to broadcast server information to all listening devices. The information sent will contain information like the URL of a WCF service, the namespace, a list of required arguments etc. What I have right now is a sender and receiver that can talk perfectly fine when they are on the same computer. However, once I put the sender on another computer than my receiver, the sender sends its message but my receiver never gets it. There are no exceptions being thrown, and the firewall is disabled on both machines.

http://codeidol.com/csharp/csharp-network/IP-Multicasting/What-Is-Broadcasting/ is where I got my code from.

Sender:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace UDPTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
            ProtocolType.Udp);
            sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
            IPEndPoint iep = new IPEndPoint(IPAddress.Broadcast, 9050);
            byte[] data = Encoding.ASCII.GetBytes("This is a test message");
            sock.SendTo(data, iep);
            sock.Close();
        }
    }
}

Receiver:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace UDPBroadcastReciever
{
    class Program
    {
        static void Main(string[] args)
        {
            Socket sock = new Socket(AddressFamily.InterNetwork,
            SocketType.Dgram, ProtocolType.Udp);
            IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9050);
            sock.Bind(iep);
            EndPoint ep = (EndPoint)iep;
            Console.WriteLine("Ready to recieve");
            byte[] data = new byte[1024];
            int recv = sock.ReceiveFrom(data, ref ep);
            string stringData = Encoding.ASCII.GetString(data, 0, recv);
            Console.WriteLine("Received: {0} from: {1}", stringData, ep.ToString());
            sock.Close();
            Console.ReadLine();
        }
    }
}

Does anyone know of anything I am missing that would enable these two to talk on two different computers? They are on the same subnet (192.168.1.x)

Thanks Nick Long


Solution

  • You would probably be better off using multicast rather than broadcasting; broadcast packets are often dropped immediately by routers. Pick an IP address somewhere in the 239.0.0.0/24 block as your multicast address; this is reserved for organisation local messages, so just pick a number out of the air and stick with it.

    You need to have your sender send its packets to this address and have your receiver join the multicast group to receive them. To join the multicast group call this on your socket:

    sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership,
        new MulticastOption(theMulticastIp));
    

    There's plenty more information about using multicast in C# here.