0
votes

When I start multiple instances of application listening on the same UDP port, only the last receives datagrams. Is there any way to send data to all instances other than sending broadcast (broadcasts are received in all instances)?

My application (commandline):

UdpClient client = new UdpClient();
client.Client.SetSocketOption(
SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
client.Client.Bind(new IPEndPoint(0, 7894));
client.EnableBroadcast = true;

Console.WriteLine("Press key to send datagram");

while (true)
{
    if (client.Available > 0)
    {
        IPEndPoint ep = null;
        client.Receive(ref ep);
        Console.WriteLine("Received");
    }
    if (Console.KeyAvailable)
    {
        Console.ReadKey();
        client.Send(new byte[] { 1 }, 1, new IPEndPoint(IPAddress.Parse("127.0.0.1"), 7894));
        Console.WriteLine("Sent");
    }
}
1
Is there any way to send data to all instances other than sending broadcast Yes. read about multicastEser

1 Answers

-1
votes

A port can only been owned by one application/process at a time, so a datagram sent to that port can only be received by one instance.

A clean way would be for each listener to use an ephemeral port and register it with the sender application.