I have a project that requires a client app to communicate with a server. I was wondering if there is a way for the client app to detect available servers and "know" by some method or another which server is the one it needs.

The client is built with .net and c#, the server is a simple SQL database.They are on the same network.

Is there a built in mechanism to do this or is the task more complex then it seems at first?

I don't have more details ATM, it's still just an idea. The reason behind it is that when a user uses the client app, we don't want him to enter connection strings manually if he doesn't have to.

  • Usually, connection string is placed to App.config file. Google: "c# app.config connection string" – astef May 23 '14 at 14:48
  • 1
    Not much experience with it directly, but I believe you'll want to do some sort of UDP broadcast. Here's a similar question I found on StackOverflow that may give you a starting point. stackoverflow.com/questions/210446/… – Matt Klinker May 23 '14 at 15:13
  • They are on the same network. In this case you can use broadcast packets pretty easily, but this can be fragile with things like Wifi client isolation. – Phoshi May 23 '14 at 15:54

I can think of three main solutions to the issue:

  1. Send a message to the network's broadcast address on a well-known port. Your same-network server picks this up and sends a reply to the source address ("I'm here at: ...").

  2. Make your server occasionally send a message to the broadcast address on a well-known port, once every so many seconds. Clients can listen for this and then connect.

  3. Have a master server responsible for coupling the two: the server sends "I'm not dead yet" packages to the master; client connects to master and gets a server list. (*) I don't recommend this.

1 and 2 are essentially the same, but with active (1) or passive (2) scanning. 1 seems cleaner on the network; 2 is probably a little easier to implement.

(*) This is a lot more hassle and requires another server on a well-known address, and represents a single point of failure, but it can solve discoverability issues.


Speaking on a higher level than JvR, I would use an existing library that uses broadcast/multicast internally.

I'm not as familiar with the Windows ecosystem, but this would be solved by a zeroconf implementation: either Bonjour (Mac OS X) or Avahi (Linux/BSD). The only thing I've ever used Avahi for myself is mDNS, but I know it supports service discovery as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.