Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi experts,

whats the best way to let a tcp client connect to an unknown tcp Server. That means i have multiple Clients in my Network and multiple Servers application, now i want that all Clients connect automatically to one of the servers. First i tried using UDP and broadcast the server address and port, this works fine as long as I have only one Client on each machine running. When i have 2 Clients for example on the same machine running the UDP way would not work anymore because of the restriction that each udpsocket can only be listened by one application.

Any ideas how i can implement such an server info broadcast without udp?

Kind Regards
Manu
Posted

1 solution

One option would be to make the clients go into a listen - sleep - wake - try port - listen cycle. Each client would wake up, try to bind to the udp port, if it fails (due to the port already being in use) go back to sleep again for a random duration. If it is able to bind to the port then listen for a duration for the udp broadcast. Once it has received the broadcast do what ever it needs to do and release the udp for others to use.

This way, multiple clients on the same machine would take turns listening for the udp packet with the server information through the one port.

All clients would then eventually locate and connect to the broadcasted server.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900