C
C#3y ago
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)}");
}
}
3 Replies
DapperDeer
DapperDeerOP3y ago
The exception that's thrown when trying to spin up additional listeners is: Unhandled exception. System.Net.Sockets.SocketException (10048): Only one usage of each socket address (protocol/network address/port) is normally permitted.
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Accord
Accord3y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server