Receive UDP broadcast packets in C #
Reference article
https://garafu.blogspot.com/2015/08/broadcast.html
Implementation
This is a sample that makes the task of receiving and echoing UDP broadcast packets resident.
using System.Net;
using System.Net.Sockets;
namespace udp
{
public class udpSample {
public void start()
{
Task.Run(() => BroadcastReceiver()); //UDP broadcast packet reception task activation
}
private void BroadcastReceiver()
{
var endPoint = new IPEndPoint(IPAddress.Any, 4000); //IP Address to monitor all addresses.Specify Any
using (UdpClient udpClient = new UdpClient(endPoint)) {
while(true) {
var buff = udpClient.Receive(ref endPoint); //Receive (synchronous)
udpClient.Send(buff, buff.Length, endPoint); //Echo the received content
}
}
}
}
}
IPEndPoint has ʻusing System.Net;
UdpClient requires ʻusing System.Net.Sockets;
.