1

I am trying to send broadcast message in network using UDP client in C#, but i don't see message received by any listener client in network, how can i troubleshoot?

I am trying to communicate with unknown IP (micro controller device)/PC, hence i am using broadcast proprietary message on UDP broadcast. i have need to get remote device static IP hence i am using broadcast.

Sender code


IPAddress broadcast = null;

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

s.EnableBroadcast = true;

// proprietary message for identification 
byte[] sendbuf = {0x5C, 0x45, 0x6D, 0x62, 0x65, 0x64,
            0x4C, 0x6F, 0x67, 0x69, 0x6B, 0x5, 0x6C, 0x73, 0x74, 0x5, 0x4, 0x5};

broadcast = IPAddress.Parse("255.255.255.255");

IPEndPoint ep = new IPEndPoint(broadcast, 1110);

s.SendTo(sendbuf, ep); // send message

I used the code from Microsoft site and modified for the IP to 255.255.255.255, however when i use IP 192.168.2.255 (router IP 192.168.2.1) the device (micro controller based device) responds, i also checked with UDP listener on another PC in network, but i dont see listener receiving(another PC) message not also on wireshark when ip is set as 255.255.255.255 for broadcast. To experiment i turned off router and connected PC(sender) and PC(listener) to just switch but there was no message of received on listener.

when i connect device (micro controller device) directly to PC, it works on both 255.255.255.255 and 192.168.2.255. i dont understand how to troubleshoot when device is in network and why broadcast doesnt work in network.

need help to resolve this ? in case any point is unclear please notify

EDIT 1: i changed to code from micro controller to output sendto(255.255.255.255) Now when i use directed broadcast (192.168.2.255) i can see both outgoing message from application and as well as incoming message to application (from MCU (255.255.255.255)) on wire shark

but when i use IP 255.255.255.255 on application for sending message i don't see any packet on wire shark.

Edit2: However i believe it C# or windows that is blocking UDP broadcast,i tried the application on Win7 as well as win10.

htadmin
  • 23
  • 3
  • You router may be blocking broadcasting of packages. When a router receives a packet, it gets inspected, then forwarded out the appropriate interface or it gets dropped. When a router receives a broadcast packet, it drops it (excluding directed-broadcasts, dhcp, etc). – Pablo Recalde Sep 10 '19 at 09:06
  • @PabloRecalde hi pablo thanks for reply, i also tried without router , i switch off power of router than only switch and PC and device were connected, PCs to switch and Device to switch. then also it failed – htadmin Sep 10 '19 at 09:08
  • Beware some switchs also block broadcasting – Pablo Recalde Sep 10 '19 at 09:26
  • I meant switches* – Pablo Recalde Sep 10 '19 at 09:32

1 Answers1

0
    // creat local endpoint on local ip to bind socket
IPEndPoint localEP = new IPEndPoint(localBind, 32350);
s.Bind(localEP); //  need to bind to a local IP address

AS mentioned in this thread Windows 7 handles 255.255.255.255 broadcast in a different way

we need to bind to a local IP address in windows 7 or later version.

htadmin
  • 23
  • 3