1

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

nick
  • 2,833
  • 3
  • 34
  • 61
  • I believe not every router will forward broadcast packets. Maybe you can check this with a different application which is known to work. – usr Feb 23 '12 at 22:39
  • Do you have a suggestion for a sample program? – nick Feb 23 '12 at 22:43
  • 2
    Also check C:\Windows\System32\LogFiles\Firewall and see if the profile for the network settings is blocking the port you are broadcasting on – JeremyK Feb 23 '12 at 22:43
  • Would also suggest using wireshark to make sure you are getting the message – JeremyK Feb 23 '12 at 22:44
  • Nothing in the logfiles directory. When I copy / paste the code found http://msdn.microsoft.com/en-us/library/tst0kwb1.aspx there it works fine. – nick Feb 23 '12 at 22:57
  • 2
    @nick Could be because you are broadcasting to 255.255.255.255 (`IPAddress.Broadcast`) in your code and the code on that page is broadcasting to 192.168.1.255. – spencercw Feb 23 '12 at 23:01
  • @spencercw Once I changed that it works fine for broadcasting over the LAN. The next thing I need to get is so it broadcasts over the internet so that our app on the android can know where to upload the images. I originally had it hard-coded, but my boss wants a beacon. – nick Feb 23 '12 at 23:33
  • 1
    @nick Not going to happen. Multicast and broadcast packets won't make it out of your internal network (and if they do, they will stop the moment they hit your ISP's routers). Your best bet is probably to set up an HTTP server that your app can periodically connect to to update whatever you need it to. Of course, if you do this you're going to be hardcoding that address anyway, so there's probably no point. – spencercw Feb 23 '12 at 23:36
  • @spencercw That's what I thought. I guess I'll get it built and then tell him it will only work on our internal network. We already have an http server that hosts the service it should connect to, with a domain name and the location of the app isn't going to change so I'm not sure what the benefit is of setting something like this up is anyway – nick Feb 24 '12 at 16:54

2 Answers2

1

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.

spencercw
  • 3,320
  • 15
  • 20
0

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;

user3130012
  • 449
  • 1
  • 3
  • 12