C# UDP broadcast client/server does not work

19,044

Solution 1

Try a broadcast on the local subnet only. IE if your subnet is 255.255.255.0 try a broadcast of 172.16.75.255. It may be that windows, a router or even network card automatically block universal broadcasts as a preventative measure.

Solution 2

Is the client on the same physical network as the server? If not, you won't be able to do a local broadcast (255.255.255.255) and will need to do a directed subnet broadcast. You will have to enable your router to allow a directed subnet broadcast (172.16.75.255) before that will work.

Solution 3

I had the similar problem, nothing seemed to work in all the code I saw here. when I started my program there was this firewall window that pops up telling you the firewall has blocked some features.

my problem was I was clicking allow access without ticking the check box that says "Private networks, suck as my home or work network". you can change this later of course in your firewall setting and tick that check box. both de server and the other machine must have that check box checked. Or at least that's what makes my mine work.

Also i had to change my broadcast IP address to for example 192.168.1.255. My router does block the recommended by my book 224.0.0.0 - 239.255.255.255;

Share:
19,044
Craig
Author by

Craig

Updated on June 26, 2022

Comments

  • Craig
    Craig almost 2 years

    I'm using .NET 2.0 and have created a fairly simple udp broadcast app and UDP listener.

    Listener code:

    Socket listener = new Socket( AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp );
    IPEndPoint localEndPoint = new IPEndPoint( IPAddress.Any, 11000 );
    listener.Bind( localEndPoint );
    EndPoint ep = (EndPoint)localEndPoint;
    Console.WriteLine("Ready to receive…");
    byte[] data = new byte[1024];
    int recv = listener.ReceiveFrom(data, ref ep);
    string stringData = Encoding.ASCII.GetString(data, 0, recv);
    Console.WriteLine("received: {0} from: {1}", stringData, ep.ToString());
    listener.Close();
    

    Server code:

    int groupPort = 11000;
    IPEndPoint groupEP = new IPEndPoint( IPAddress.Parse( "255.255.255.255" ), groupPort );
    
    if ( radioButton2.Checked )
    {
        groupEP = new IPEndPoint( IPAddress.Broadcast, groupPort );
    }
        else if ( radioButton3.Checked )
    {
        groupEP = new IPEndPoint( IPAddress.Parse( "172.16.75.15" ), groupPort );
    }
    
    Socket socket = new Socket( AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp );
    socket.SetSocketOption( SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1 );
    socket.SendTo( System.Text.ASCIIEncoding.ASCII.GetBytes( testTextBox.Text ), groupEP );
    

    The server is just a simple windows app with 3 radio buttons, button and a textbox.

    When I run the server on a separate computer and choose radioButton3 I receive the message just fine on my client listener (which is running on ip address 172.16.75.15). However if I choose the first or second radiobutton (which creates Broadcast or 255.255.255.255 as the ip address) I get nothing. Now if I run the client on the same pc as the server I can receive the message using those two choices.

    I'm not sure what I'm doing wrong or if there could be some kind of firewall preventing UDP messages on the LAN. Any ideas?

    Thanks,

    Craig

    • John Saunders
      John Saunders almost 15 years
      Craig: maybe it would work if you were using VB.NET?
    • nos
      nos almost 15 years
      One thing you can do is check wether the packet gets out on the net, to know if it's a problem on the client or server. Install a network monitor such as wireshark and monitor your LAN.
    • John Saunders
      John Saunders almost 15 years
      Yeah, but that won't work with C#. Craig says so.
    • Craig
      Craig almost 15 years
      John, what's with the C#/VB.NET comments? Issues?
  • Josh E
    Josh E almost 15 years
    most routers / switches / etc. block the propagation of broadcasts by default.
  • Craig
    Craig almost 15 years
    I've tried the 172.16.75.255 broadcast and it failed too. Josh you could very well be right.
  • Jesse Chisholm
    Jesse Chisholm almost 12 years
    PROS of Multicast: friendlier netiquet as only those that join the group will even see your packet in their IP stack, routers are less likely to block it. CONS of multicast: There is a little bit more work setting up your socket. PROS of broadcast: trivial to set up the socket. CONS of broadcast: every machine within your TTL hop limit must process the message, routers tend to block broadcast from jumping subnets.
  • Massimo
    Massimo almost 11 years
    I disagree. I manage 1200 pc clients with udp broadcast, they are on the same subnet. Dhcp uses udp broadcast, also over different subnets ( req. router with dhcp relay ). It depends on your design and goals ... and not always you have multicast on the routers !