开发者

How to connect multiple client to server [closed]

开发者 https://www.devze.com 2023-02-24 02:07 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the hel开发者_C百科p center. Closed 11 years ago.

I have two application one is Client and other is sever , how client application automatically detect the sever application if sever application is running

please tell me best method for this


If you know the IP of the server, then try and connect, if its just "running somewhere" on your local network you could send a broadcast

An example: (not my code, borrowed)

  public static void Main()
  {
   Advertise server = new Advertise();
  }
  public Advertise()
  {
   Thread advert = new Thread(new ThreadStart(sendPackets));
   advert.IsBackground = true;
   advert.Start();
   Console.Write("Press Enter to stop");
   string data = Console.ReadLine();
  }
  void sendPackets()
  {
   Socket sock = new Socket(AddressFamily.InterNetwork,
           SocketType.Dgram, ProtocolType.Udp);
   sock.SetSocketOption(SocketOptionLevel.Socket,
              SocketOptionName.Broadcast, 1);
   IPEndPoint iep = new IPEndPoint(IPAddress.Broadcast, 9050);
   string hostname = Dns.GetHostName();
   byte[] data = Encoding.ASCII.GetBytes(hostname);
   while (true)
   {
     sock.SendTo(data, iep);
     Thread.Sleep(60000);
   }
  }

The client then listens for the broadcast, if it receives 1 or more responses it could offer the user a what do you want to connect to.

This of course only works on a local network (with no firewalls/subnets)

Otherwise, you have to ask the user where the server is


Yours application tries to connect to the server.
The server replies.
The server is running.

Yours application tries to connect to the server.
The server does not reply.
The server is not running or there is some other problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号