1
votes

I have several LANs(10.0.0.x) connected to a WAN(192.168.1.x). Each through a router that allows a network directed broadcast. This is to allow the devices in the LANs to be discovered by devices on the WAN.

I can send a broadcast on the LAN(10.0.0.255) and receive it on my socket. But when I move to the WAN I can see it in wireshark, but not my socket. I other words I have a device with address 10.0.0.1 sending the same message to 192.168.1.255 through a gateway but my socket is not receiving it. When this happens the source address is the address of the router. Here is the code for my socket:

                    udpSocket = new Socket(AddressFamily.InterNetwork,
                        SocketType.Dgram, ProtocolType.Udp);

                    //Assign the any IP of the machine and listen on port number 5000
                    IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, 5000);

                    //Bind this address to the server
                    udpSocket.Bind(ipEndPoint);

                    IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 5000);
                    //The epSender identifies the incoming clients
                    EndPoint epSender = (EndPoint)ipeSender;

                    //Start receiving data
                    udpSocket.BeginReceiveFrom(byteData, 0, byteData.Length,
                        SocketFlags.None, ref epSender, new AsyncCallback(ReceiveAnnounce), epSender);

I have a wireshark trace for each message but I'm not sure the best way to post it. Thanks.

1

1 Answers

0
votes

Do you understand that UDP provides no guarantee that a packet will be received? (TCP uses a client to server connection) and that packets are broadcast to a 'multicast group' (this resembles an IP-address but must be in the range 224.0.0.0 to 239.255.255.255) rather than to an IP.

Your use of IPAddress.Any tells it to use all network interfaces, but you never tell it which multicast group to use, I've never done this in C# myself, however it appears you want to add this line of code before your BeginReceiveFrom;

udpSocket.SetSocketOption(SocketOptionLevel.IP,SocketOptionName.AddMembership,new MulticastOption(TARGET_IP)); 

You should replace TARGET_IP with the address of your multicast group you wish to listen on.

I took that line of code from this site; http://osix.net/modules/article/?id=409