Hello,
I am building an intercom class library that will be included in many of our apps. The intercom will be responsible for creating a Tcp a connection between itself and another app running the intercom on the same network (or computer.) When the intercom is running, it needs to be able to TX and RX to any number of other apps running the intercom. The problem I'm having is getting 2 apps to navigate the port selection together. Since a port can only be used for a listener once on any machine, if an app is connecting to 3 other apps, it needs to use 3 different ports (at least that's how I understand it.)

Every app uses a Mutex to make it a guarantee it runs only once on any computer at any time.

My approach to this to has the intercom class creating a Udp listener on a port that is selected from a range of ports (49000-49010). The intercom will also broadcast a hello Udp packet on every port in the range at 15 second intervals.The payload for this message will include a unique identifier for the app instance, and all of the available ports between 50000 and 50200 on the computer hosting the app.

The communication between 2 apps to negotiate the connection goes like this...

App1 - Hello here are my available ports
App2 - I've looks at your ports. Please connect to me on these Rx and TX ports
App1 - Done. These are the port I connected to.
App2 creates the connection. done

I am finding a few pitfalls, the biggest being the Udp packets are not guaranteed to arrive and if they do, they might not be in the correct order. Also, if there are multiple apps already running and others come online and says hello, all existing apps reply at the same time. This is causing synchronizing issues and headaches.

I have considered having a dedicated port for each app, but since the apps may run on the same machine, this would fail since a port can be used as a listener only once on a computer.

Ultimately, my approach to this seems to be getting exponential complex which in my experience is one of the first signs of a bad approach so I'm thinking there is a better way.

Does anyone have any experience getting their apps to communicate with each other? and if so what is the approach you have used?
Thanks for looking
kevin