How to connect multiple client to server [closed]
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.
精彩评论