Reputation: 1646
Requirements (Can not be modified)
I have a windows forms application. When this application runs, it listen to a UDP port.
Users can loggin to windows and open the same application and run it, and then switch windows user without logging out and open the same application and run it.
Problem
The two applications are listening to the same port by using
SocketOptionName.ReuseAddress
as shown in this thread. But only one can get the data.
Question
There is some way so that the application of another user can read the data?. otherwise, can I listen somehow an event that notifies me about the windows user switch?
Edit
Here is the code used for setting up the listener
IPEndPoint localEndPoint = new IPEndPoint(localAddress, listenPort);
UdpClient udpListener = new UdpClient();
udpListener.ExclusiveAddressUse = false;
udpListener.Client.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.ReuseAddress, true);
udpListener.Client.Bind(localEndPoint);
IPEndPoint ep = new IPEndPoint(IPAddress.Any, 0);
byte[] answer = udpListener.Receive(ref ep);
Upvotes: 0
Views: 662
Reputation: 182761
One option is to switch to a broadcast/multicast design. This is the only direct way to allow the very same datagram to go to more than one socket.
Alternatively, go to a master/client design. Try to bind to the port. If you can, great, you're the master. If you fail, see if there's a master and connect to it.
Pick a second port for the master to use to communicate with clients. Follow this logic.
Try to open the port. If you succeed, you are the master. Also listen on the master port.
If you fail, open a random port. Send a "register client" datagram to the master.
The master must listen for "register client" datagrams on the master/client port. If it receives one, it should add the source IP and port to its list of clients. It must repeat all datagrams it receives on the main port to each of its clients.
Non-masters should repeat "register client" datagrams at a defined interval. The master should drop any client that hasn't sent a "register client" datagram in sufficiently long, say twice the defined interval.
One ugly bit -- if the master might quit, some client will have to promote itself to master. You may just want to go with a dedicated master if you can run it on the machine.
Upvotes: 1