Receiving UDP Broadcast Message in C #

I know this question has been asked many times. I read ALL the answers and tried a piece of EVRY code that I could find. In a few days, I so desperately want me to ask you for help.

I have a device and a PC in my home network. The device sends UDP broadcast messages. On my PC, I see these messages in wirehark:

Final source length

192.168.1.102 0.0.0.0 UDP 60 Source port: 9050 Destination port: 0

This means that the packets are coming to my computer. The next step was to create a C # application that receives these packages. As mentioned above, I tried all possible solutions, but it won’t get anything.

So, I think that there must be something very basic that I am doing wrong. Can someone help me? Thanks!

+6
source share
2 answers

Just survived the same problem and wanted to share what fixed it for me.

In short: it seems that the Windows firewall was somehow the cause of this strange behavior, and simply disabling the service does not help. You must explicitly allow incoming UDP packets for a specific program (executable file) in the list of incoming Windows firewall rules.

To describe the full case, read on.

My network setup: the IP of my (receiving) machine was 192.168.1.2, the IP address of the sending machine was 192.168.1.50, and the subnet mask on both machines was 255.255.255.0. My machine is running Windows 7 x64.

This is the code (approximately) that I used:

Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); IPEndPoint iep = new IPEndPoint(IPAddress.Any, 0); sock.Bind(iep); sock.EnableBroadcast = true; EndPoint ep = (EndPoint)iep; byte[] buffer = new byte[1000]; sock.ReceiveFrom(buffer, ref ep); 

This initially did not work unless I sent a broadcast packet from this socket before I call ReceiveFrom on it. That is, adding this line before calling ReceiveFrom :

 sock.SendTo(someData, new IPEndPoint(IPAddress.Broadcast, somePort)) 

When I did not send the broadcast packet first from the receiving socket, the incoming broadcast packets were not accepted by them, although they appeared in Wireshark (the packet destination was 255.255.255.255).

I thought it was like a firewall messing around with incoming packets (unless some UDP hole opens first with an outgoing packet - even if I haven't heard before punching UDP holes is applicable to broadcast packets in some way) so I went to services and turned off the Windows Firewall service in general. This has not changed anything.

However, after trying everything else, I turned on the firewall service again and tried to start the program again. This time a firewall prompt appeared asking me if I want to allow the MyProgram.vshost.exe process (I was debugging Visual Studio) through the firewall, I accepted it, and voila - it worked! Incoming packages have been received now!

+1
source

You are fine, they have something to do with the code causing the problem. (I did not read the article, just copied it)

It always works from the local machine, but for some reason it does not work from the remote machine.

To fix this: In Broadcst.cs they are broadcast twice. once for the local host and then for the destination IP address (iep2). just delete

 sock.SendTo(data, iep1); 

and it should work.

I do not know why.

0
source

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


All Articles