Denis Schaf
Denis Schaf

Reputation: 2739

Multible programs/instances using the same UDP Port in C#

I am struggling with a bit of network magic and hoped someone would be able to explain me what is happening.

I am trying to reuse udp ports. So if I have multible programs listening on the same udp port i want both of the applications to receive the data send by a different device.

Using the following code I'am able to achive just that:

            IPEndPoint localEndoint = new IPEndPoint(IPAddress.Any, 67);    //the local endpoint used to listen to port 67
            //Create a new UDP Client and bind it to port 67
            DhcpSniffer = new UdpClient();
            DhcpSniffer.ExclusiveAddressUse = false;    //Allow multible clients to connect to the same socket
            DhcpSniffer.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); // Connect even if socket/port is in use
            DhcpSniffer.Client.Bind(localEndoint);
            DhcpSniffer.Client.ReceiveTimeout = Timeout;
            //receive on port 67 
            dhcpPacket = DhcpSniffer.Receive(ref localEndoint);

Both of my programs can listen to DHCP messages in the network and don't block each other. Now i want to do the same thing with port 15120 where a RTP video stream is streamed to. However this does not work. I am using the same code but with no success only one application at a time can receive the stream, the other will run in a timeout.

            IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, port);
            //Create a new UDP Client and bind it to port 15120
            udpReceiver = new UdpClient();
            udpReceiver.ExclusiveAddressUse = false;    //this is an attempt to receive the stream on mutlible instances...this works for DHCP but not for RTP for some reason....
            udpReceiver.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); // Connect even if socket/port is in use
            udpReceiver.Client.ReceiveTimeout = timeout;    //set the sockete timeout
            udpReceiver.Client.Bind(RemoteIpEndPoint);      //bind to the port from any IP
            //receive packets on port 15120
            Byte[] receiveBytes = udpReceiver.Receive(ref RemoteIpEndPoint);

I hope somebody is able to shine a light on my confusion

Update: I found out it works with DHCP because it is send to the broadcast IP (255.255.255.255). Now I need to find out how i can change the Socket behaviour to treat my RTP stream as if it was broadcasted so i can see it in two application at the same time. (Yes I could configure my stream-source to broadcast, but this is not the goal of this). The goal is to reconfigure the Socket to behave as explained. Not to save the stream on a harddrive or redirect it using the local host.

Upvotes: 4

Views: 1228

Answers (2)

Denis Schaf
Denis Schaf

Reputation: 2739

It is not possible with multible programs to access the data from a unicast UDP package, it works only with multicast, there is no "easy" way around this by reconfiguring the UdpClient

Upvotes: 0

Alex Boutin
Alex Boutin

Reputation: 187

First, its not possible to have multiple programs listen on the same port (As far as I know it's a big security conflict)

What you can do tough, is use a NetworkManager that listen on you port (Lets call it port 8080) who will then redirect the information to you apps ports (App1 could use port 8081 and App2 use port 8082). Either you write your own, using Flask to listen on 8080 and then rerouting the package to localhost:8081 and localhost:8082 could be a simple and fast solution.

Doing this would help you secure the network and you can redirect to as many ports as you need, pretty much like a docker swarm would balance the incoming network to its cluster.

Upvotes: 1

Related Questions