Receive UDP Broadcast

I need to get a UDP translation (in Ubuntu, if that matters). Using Wireshark, I see that the packet is being sent from the server machine, and I see that it was received on my client machine, but my program is completely oblivious. This is what I have:

sockaddr_in si_me, si_other; int s; assert((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))!=-1); int port=6000; int broadcast=1; setsockopt(s, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof broadcast); memset(&si_me, 0, sizeof(si_me)); si_me.sin_family = AF_INET; si_me.sin_port = htons(port); si_me.sin_addr.s_addr = INADDR_ANY; assert(::bind(s, (sockaddr *)&si_me, sizeof(sockaddr))!=-1); while(1) { char buf[10000]; unsigned slen=sizeof(sockaddr); recvfrom(s, buf, sizeof(buf)-1, 0, (sockaddr *)&si_other, &slen); printf("recv: %s\n", buf); } 

It is compiled in debug mode, statements are not erased during compilation, and my program simply blocks on recvfrom .

Is there any other hoop that I have to jump with to get the wrong UDP transmission?

Edit: a little more information, I have two computers connected to a dedicated switch, without external interference. I also have a second network card on my client computer that connects to the company’s network, which also works.

I can ping both external (Internet browsing) and my server (plus I can see the actual packets in Wireshark), but you never know what might cause this problem.

+6
source share
2 answers

As it turns out, my code works just fine, as I thought. There was a problem with the network setup itself.

For posterity, I installed two static IP'd computers on my own hub instead of using the built-in DHCP server on the server machine to distribute the IP address for another computer. Pretty localized for my problem, but you never know.

+2
source

To send and receive broadcast

  • Make sure the netmask is correct. in the windows mask for broadcast packets does not matter, but not on Linux.

  • bind socket to INADDR_ANY

  • setsockopt to BROADCAST

  • calling sendto with sendaddr.sin_addr.s_addr = inet_addr ("your_interface_broadcast_address"). or - call several times for each interface with its broadcast IP address.

  • call recvfrom. anytime before calling recvfrom, set the length parameter.

+1
source

Source: https://habr.com/ru/post/925851/


All Articles