Exemplo n.º 1
1
        /// <summary>
        /// This method will constantly run as a thread. It currently sends a multicast to the network every second, informing
        /// computers on the network that it is there. When a client sees this, it will send a message to the server and the client
        /// will be added to the clients list.
        /// </summary>
        public static void AddClients()
        {
            clients = new List<ClientThread>();
            TcpListener serverSocket = new TcpListener(IPAddress.Any, 8888);
            serverSocket.Start();

            while (true)
            {
                TcpClient tempClientSocket = new TcpClient();
                tempClientSocket.ReceiveTimeout = 300;

                SendMulticast();

                //Check and see if a computer is trying to connect.
                //If not, then sleep, and resend multicast in a second
                if (serverSocket.Pending())
                {
                    tempClientSocket = serverSocket.AcceptTcpClient();
                    ClientThread c = new ClientThread(tempClientSocket,null);
                    clients.Add(c);
                    Console.WriteLine("Connected to " + c.GetClientIP() + " :: "+c.GetPort());
                }
                else {
                    Thread.Sleep(1000); //Sleep for a second, before sending the next multicast.
                }
            }
        }
Exemplo n.º 2
0
        public void AddClients()
        {
            clients = new List<ClientThread>();
            TcpListener serverSocket = new TcpListener(IPAddress.Any, 8888);
            serverSocket.Start();
            TcpListener heartBeatListener = new TcpListener(IPAddress.Any, 8889);
            heartBeatListener.Start();
            while (true)
            {
                TcpClient tempClientSocket = new TcpClient();
                TcpClient heartBeatSocket = new TcpClient();
                tempClientSocket.ReceiveTimeout = 300;

                //SendMulticast();
                SendBroadcast();

                //Check and see if a computer is trying to connect.
                //If not, then sleep, and resend multicast in a second
                if (serverSocket.Pending())
                {
                    tempClientSocket = serverSocket.AcceptTcpClient();

                    heartBeatSocket = heartBeatListener.AcceptTcpClient();

                    ClientThread c = new ClientThread(tempClientSocket, heartBeatSocket);
                    Thread checkHeartBeat = new Thread(() => CheckHeartBeat(heartBeatSocket,c));
                    checkHeartBeat.Start();
                    string compid = c.GetCompID().Trim();
                    //Is New
                    if (!checkNewClient(c))
                    {

                        this.objectListView1.AddObject(c);
                        try
                        {
                            AddClientToDatabase(c);
                            db.InitialSetUptimes(c.GetCompID(), c.GetUptimeHours());
                        }
                        catch (System.Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }
                    //It is Known Khaleesi
                    //It is known
                    else
                    {
                        changeOfflineclient(compid);

                    Console.WriteLine("Connected to " + c.GetClientIP() + " :: " + c.GetPort());

                        this.objectListView1.AddObject(c);

                        string group = db.GetClientGroup(compid.Trim());

                        foreach (Group g in groups)
                        {
                            if (g.GetID().Trim() == group)
                            {
                                c.SetGroup(g);
                            }
                        }

                        clients.Add(c);
                        SetStartGroups();

                    }

                }
                else
                {
                    Thread.Sleep(1000); //Sleep for a second, before sending the next multicast.
                }
            }
        }