© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
4 replies
DapperDeer

❔ Single UDP Broadcaster to Multiple Clients

I'm trying to have a single UDP Broadcast to many clients.

This is what I have working so far. Ideally, I'd like to be able to spin up multiple instances of the Listener and have them receive the message.

That is, my Broadcaster sends messages, a single listener can connect and receive, but any additional listeners throws an exception.
Broadcaster:
using (var udpClient = new UdpClient { ExclusiveAddressUse = false, EnableBroadcast = true})
{
  var ep = new IPEndPoint(IPAddress.Parse(this._options.BroadcastAddress), this._options.BroadcastPort);
  ... // encodedMessage is created here
  while (true)
  {
    Console.WriteLine($"Broadcasting encoded message:\n {JsonSerializer.Serialize(broadcastMessage)}");
        udpClient.Send(encodedMessage, ep);
  }
}
using (var udpClient = new UdpClient { ExclusiveAddressUse = false, EnableBroadcast = true})
{
  var ep = new IPEndPoint(IPAddress.Parse(this._options.BroadcastAddress), this._options.BroadcastPort);
  ... // encodedMessage is created here
  while (true)
  {
    Console.WriteLine($"Broadcasting encoded message:\n {JsonSerializer.Serialize(broadcastMessage)}");
        udpClient.Send(encodedMessage, ep);
  }
}

Listener:
using (var udpClient = new UdpClient { ExclusiveAddressUse = false, EnableBroadcast = true})
{
  var ep = new IPEndPoint(IPAddress.Parse(this._options.BroadcastAddress), this._options.BroadcastPort);
  this._udpClient.Client.Bind(ep);
  while (true)
  {
    var message = udpClient.Receive(ref ep);
    Console.WriteLine($"Received: {Encoding.Utf8.GetString(message)}");
  }
}
using (var udpClient = new UdpClient { ExclusiveAddressUse = false, EnableBroadcast = true})
{
  var ep = new IPEndPoint(IPAddress.Parse(this._options.BroadcastAddress), this._options.BroadcastPort);
  this._udpClient.Client.Bind(ep);
  while (true)
  {
    var message = udpClient.Receive(ref ep);
    Console.WriteLine($"Received: {Encoding.Utf8.GetString(message)}");
  }
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ MSAL multiple graph clients
C#CC# / help
3y ago
How can I accept multiple clients?
C#CC# / help
2y ago
❔ TCP vs UDP
C#CC# / help
3y ago
❔ Change UDP Port
C#CC# / help
4y ago