Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way for a client app to find a server on a local network in C#?

The client connects to the server using GenuineChannels (we are considering switching to DotNetRemoting). What I mean by find is obtain the IP and port number of a server to connect to.

It seems like a brute-force approach would be try every IP on the network try the active ports (not even sure if that's possible) but there must be a better way.

like image 621
Davy8 Avatar asked Oct 16 '08 21:10

Davy8


People also ask

What should client use to contact server?

To connect to the server, the client places the port number and the IP address of the server into a sockaddr_in structure like the bind() call. If the client does not know the server IP address, but it does know the server host name, the gethostbyname() call is called to translate the host name into its IP address.

How do clients communicate with servers?

Clients typically communicate with servers by using the TCP/IP protocol suite. TCP is a connection-oriented protocol, which means a connection is established and maintained until the application programs at each end have finished exchanging messages.

How do I run a client server program in C on the same machine?

Instructions to Execute :Open two terminals on your machine and compile the server and the client programs in different terminals. Run the server program first, followed by running the client program. It can be seen that the data sent by the server is printed on the terminal running the client program.

What are the client server applications?

1 What is a Client/Server Application? In principle, a client/server application consists of a client program that consumes services provided by a server program. The client requests services from the server by calling functions in the server application.


1 Answers

I'd say the best way is to use Bonjour/Zeroconf/mDNS for C#; a lot of thought went into making it play nice with the network; IE it pings less frequently over time if possible, etc. There's Mono.Zeroconf, and I read there's an older .NET project in the Apple SDK but I haven't found it.

So the easiest would be to install Bonjour for Windows, then get the Windows Binaries for Mono.Zeroconf try the example MZClient.exe drop the Mono.Zeroconf.dll and/or Mono.Zeroconf.Providers.Bonjour.dll into your project references and go.

Something like this:

var service = new Mono.Zeroconf.RegisterService {
                Name = "Use Me for Stuff",
                RegType = "_daap._tcp",
                ReplyDomain = "local.",
                Port = 0024200,
                TxtRecord = new Mono.Zeroconf.TxtRecord {
                            {"I have no idea what's going on", "true"}}
              };
service.Register();

var browser = new Mono.Zeroconf.ServiceBrowser();
browser.ServiceAdded +=
    delegate(object o, Mono.Zeroconf.ServiceBrowseEventArgs args) {
        Console.WriteLine("Found Service: {0}", args.Service.Name);
        args.Service.Resolved +=
            delegate(object o, Mono.Zeroconf.ServiceBrowseEventArgs args) {
                var s = args.Service;
                Console.WriteLine(
                    "Resolved Service: {0} - {1}:{2} ({3} TXT record entries)",
                    s.FullName, s.HostEntry.AddressList[0], s.Port, s.TxtRecord.Count);
          };
        args.Service.Resolve();
    };
browser.Browse("_daap._tcp", "local");
like image 80
dlamblin Avatar answered Sep 28 '22 05:09

dlamblin