예제 #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.
                }
            }
        }
예제 #2
0
 public AddAction(ref ScheduledTask task, ClientThread client, ServerMain dialog)
 {
     InitializeComponent();
     this.task = task;
     this.client = client;
     this.dialog = dialog;
 }
예제 #3
0
        /// <summary>
        /// Remove a client from this room, also checks if he is a drawer or a host.
        /// </summary>
        /// <param name="clientThread"></param>
        public void RemoveClient(ClientThread clientThread)
        {
            clients.Remove(clientThread);

            if (clientThread == host)
            {
                if (clients.Count > 0)
                {
                    this.MakeHost(clients[0]);
                }
                else
                {
                    this.DestroyRoom();
                    return;
                }
            }

            if (clientThread == drawer)
            {
                if (clients.Count == 1)
                {
                    gameHandler.EndGame();
                }
                else
                {
                    gameHandler.NewRound();
                }
            }

            this.SendToAllClientsInRoom(new Message(MessageTypes.Inform, JsonConvert.SerializeObject(new GuessModel(clientThread.Name + " left the room"))));
        }
예제 #4
0
 /// <summary>
 /// Add a client to the group.
 /// </summary>
 /// <param name="c">The client object to add to the group</param>
 public void AddClient(ClientThread c)
 {
     clients.Add(c);
     if (c.GetGroup() != this)
     {
         c.SetGroup(this);
     }
 }
예제 #5
0
 public ChooseGroup(ref List<Group> groups,  object client,  ServerMain ad, Database db)
 {
     InitializeComponent();
     this.db = db;
     this.groups = groups;
     this.client = (ClientThread)client;
     this.ad = ad;
 }
        public void ListenData(object obj)
        {
            //Socket clientSK = (Socket)obj;
            while (true)
            {
                try
                {
                    if (client.Connected)
                    {
                        byte[] buff = new byte[1024];
                        int    recv = client.Receive(buff);
                        if (recv > 0)
                        {
                            //HamGiaiMa(buff);
                            //txtMain.AppendText("Client: "+Encoding.UTF8.GetString(buff)+"\n");
                            txtMain.AppendText("Client: " + Encoding.ASCII.GetString(buff).ToString() + "\n");
                            //txtMain.AppendText("Client: " + buff.ToString() + "\n");
                            txtMain.ScrollToCaret();
                            //MessageBox.Show(recv.ToString());
                        }
                        else
                        {
                            connect = false;
                            DisposeSocket();
                            ClientThread.Abort();
                        }
                    }
                    else
                    {
                        connect = false;
                        DisposeSocket();
                        ClientThread.Abort();
                    }

                    //if (recv > 1)
                    //{
                    //    //HamGiaiMa(buff);
                    //    //txtMain.AppendText("Client: "+Encoding.UTF8.GetString(buff)+"\n");
                    //    txtMain.AppendText("Client: " + Encoding.ASCII.GetString(buff) + "\n");
                    //    txtMain.ScrollToCaret();
                    //    //MessageBox.Show(recv.ToString());
                    //}
                    //else
                    //{
                    //    connect = false;
                    //    DisposeSocket();
                    //    ClientThread.Abort();
                    //}
                }
                catch (Exception exp)
                {
                    //MessageBox.Show("Mat ket noi");
                    notifyIcon.BalloonTipText = "MAT KET NOI";
                    notifyIcon.ShowBalloonTip(500);
                    ClientThread.Abort();
                }
            }
        }
예제 #7
0
 /// <summary>
 /// Start the server.
 /// </summary>
 public void Start()
 {
     while (true)
     {
         TcpClient    tcpClient    = listener.AcceptTcpClient();
         ClientThread clientThread = new ClientThread(tcpClient, hub);
         clientThread.StartClientThread();
         hub.AddClient(clientThread);
     }
 }
예제 #8
0
 /// <summary>
 /// Makes a client join an other room
 /// </summary>
 /// <param name="clientThread"></param>
 /// <param name="roomname"></param>
 public void JoinOtherRoom(ClientThread clientThread, string roomname)
 {
     if (server.RoomExists(roomname))
     {
         server.JoinRoom(clientThread, this, roomname);
     }
     else
     {
         server.CreateRoom(clientThread, this, roomname);
     }
 }
예제 #9
0
 /// <summary>
 /// Create a new room, only happens if the room does not already exists.
 /// </summary>
 /// <param name="clientThread"></param>
 /// <param name="currentRoom"></param>
 /// <param name="name"></param>
 public void CreateRoom(ClientThread clientThread, Room currentRoom, string name)
 {
     if (!RoomExists(name))
     {
         currentRoom.RemoveClient(clientThread);
         Room room = new Room(this, name);
         room.AddClient(clientThread);
         rooms.Add(room);
         clientThread.JoinRoom(room);
     }
 }
예제 #10
0
        /// <summary>
        /// Start the game, can only happen if there are more than 2 people in the room.
        /// </summary>
        public async void StartGame()
        {
            if (this.clients.Count > 1)
            {
                await this.gameHandler.StartGame(clients);

                this.drawer = clients[0];
                this.SendToAllClientsInRoom(new Message(MessageTypes.NewDrawer, JsonConvert.SerializeObject(new ClientModel(drawer.Name, true))));
                this.SendToAllClientsInRoom(new Message(MessageTypes.GuessWord, JsonConvert.SerializeObject(new GuessModel(this.gameHandler.Word))));
                this.SendToAllClientsInRoom(new Message(MessageTypes.StartGame, JsonConvert.SerializeObject(new GameModel(gameHandler.Word.Length, 1))));
            }
        }
예제 #11
0
        /// <summary>
        /// Check if the entered username is available for use.
        /// </summary>
        /// <param name="username"></param>
        /// <param name="client"></param>
        public void CheckUsernameServer(string username, ClientThread client)
        {
            bool isValid = this.server.CheckUsername(username);

            if (isValid)
            {
                client.SendMessage(new Message(MessageTypes.UsernameCheck, JsonConvert.SerializeObject(new ClientModel(username, true))));
            }
            else
            {
                client.SendMessage(new Message(MessageTypes.UsernameCheck, JsonConvert.SerializeObject(new ClientModel(username, false))));
            }
        }
예제 #12
0
 public override void disconnect(bool disconnect)
 {
     if (disconnect)
     {
         _storage.SaveFile();
         sendPacket(new PacketDisconnectResponse()
         {
             disconnectOk = true
         });
         TcpClient.Close();
         ClientThread.Abort();
         Console.WriteLine("Client closed: {0}", identifier.Username);
     }
 }
예제 #13
0
        /// <summary>
        /// Adds a client to this room.
        /// </summary>
        /// <param name="clientThread"></param>
        public void AddClient(ClientThread clientThread)
        {
            clientThread.JoinRoom(this);
            clients.Add(clientThread);
            SendToAllClientsInRoom(new Message(MessageTypes.JoinRoom, JsonConvert.SerializeObject(new RoomModel(this.Name, this.clients.Count))));
            SendToAllClientsInRoom(new Message(MessageTypes.Inform, JsonConvert.SerializeObject(new GuessModel(clientThread.Name + " joined the room"))));

            if (this.Name.ToLower() != "hub")
            {
                if (clients.Count == 1)
                {
                    this.MakeHost(clientThread);
                }
            }
        }
예제 #14
0
        /// <summary>
        /// Form Constructor
        /// </summary>
        /// <param name="ProcList">Current Known Processes, Built from parent form data grid</param>
        /// <param name="client">Client for commands to be sent too</param>
        /// <param name="parent">parent form</param>
        public NewProcess(ClientThread client, ServerMain parent, Database db)
        {
            s = parent;
            List<string> ProcList = db.GetProcesses(client.GetCompID());
            ProcList.Sort();
            InitializeComponent();
            this.client = client;
            string clientName = client.GetComputerName().Split('\0')[0];
            lstBoxProcesses.Items.Clear();
            toolLblConnectedClient.Text += clientName;

            foreach(string procname in ProcList)
            {
                lstBoxProcesses.Items.Add(procname + ".exe");
            }
        }
예제 #15
0
        /// <summary>
        /// Makes a clientThread join another room
        /// </summary>
        /// <param name="clientThread"></param>
        /// <param name="currentRoom"></param>
        /// <param name="name"></param>
        public void JoinRoom(ClientThread clientThread, Room currentRoom, string name)
        {
            if (RoomExists(name))
            {
                foreach (Room room in rooms)
                {
                    if (room.Name.ToLower() == name.ToLower())
                    {
                        currentRoom.RemoveClient(clientThread);
                        room.AddClient(clientThread);
                    }
                }

                DestoryRooms();
            }
        }
예제 #16
0
        /// <summary>
        /// This will init a remote file directory
        /// </summary>
        /// <param name="path">The path to init</param>
        /// <param name="client">The client in which we are taking the file directory from</param>
        /// <param name="richTextBox1">The textbox in which we want to print the log to</param>
        public void InitRemote(string path, ClientThread client, ref RichTextBox richTextBox1)
        {
            this.log = richTextBox1;
            IsRemote = true;
            this.client = client;
            Messaging.SendCommand("return InitFTPDirectory('" + path + "');", client.GetClientSocket());
            FTPDirectory dir = (FTPDirectory)Messaging.RecieveFTPDirectory(client.GetClientSocket());

            treeListView1.CanExpandGetter = delegate(object x)
            {
                return ((FTPDirectory)x).IsFile == false;
            };
            treeListView1.ChildrenGetter = delegate(object x) { return GetRemoteChildren(x, client); };

            treeListView1.Roots = dir.Children;
        }
예제 #17
0
        /// <summary>
        /// This will init a local directory.
        /// </summary>
        /// <param name="path"></param>
        public void InitLocal(string path,ClientThread client, ref RichTextBox richTextBox1)
        {
            this.client = client;
            this.log = richTextBox1;
            IsRemote = false;
            FTPDirectory dir = new FTPDirectory(path, false, null);
            dir.GetDirs();
            dir.GetChildrenDirs();

            // Configure the first tree
            treeListView1.CanExpandGetter = delegate(object x)
            {
                return ((FTPDirectory)x).Children != null;
            };
            treeListView1.ChildrenGetter = delegate(object x) { return ((FTPDirectory)x).GetChildren(); };

            treeListView1.Roots = dir.Children;

            treeListView1.IsSimpleDragSource = true;
            treeListView1.IsSimpleDropSink = true;
        }
예제 #18
0
 public static void MainListenThread()
 {
     while (TagetValue.Run)
     {
         try
         {
             Socket       client    = TcpLiserner.AcceptSocket();
             ClientThread newclient = new ClientThread(client);
             IPEndPoint   clientip  = (IPEndPoint)client.RemoteEndPoint;
             Value.WriteLog.WriteLine("new Client:" + clientip.Address + ":" + clientip.Port, LogType.LT_Warning);
             Thread newthread = new Thread(new ThreadStart(newclient.ClientService));
             newthread.Start();
             if (!TagetValue.Run)
             {
                 client.Close(); return;
             }
         }
         catch (Exception E)
         {
             Value.WriteLog.WriteLine("MainListenThread:" + E.Message, LogType.LT_Warning);
         }
     }
 }
예제 #19
0
 public AddNewTask(ServerMain dialog, ClientThread client)
 {
     InitializeComponent();
     this.dialog = dialog;
     this.client = client;
 }
예제 #20
0
 /// <summary>
 /// Send a whole folder to a remote client
 /// </summary>
 /// <param name="ftp">The directory to send</param>
 /// <param name="pathOfFile">The location of the directory</param>
 /// <param name="pathToGo">Where to store it in the remote machine</param>
 /// <param name="client">The current client</param>
 /// <param name="oppositeFolderControl">The folder control that dropped it</param>
 private void SendFolderToClient(FTPDirectory ftp, string pathOfFile, string pathToGo, ClientThread client, FolderControl oppositeFolderControl)
 {
     if (ftp.IsFile)
     {
         pathToGo = pathToGo.Replace("\\", "\\\\");
         oppositeFolderControl.client.SendFileToOtherClient(pathOfFile, ftp.kbSize, pathToGo, client);
     }
     else
     {
         ftp.GetChildren();
         foreach (FTPDirectory f in ftp.Children)
         {
             SendFolderToClient(f, f.Path, pathToGo + "\\" + f.Name, client, oppositeFolderControl);
         }
     }
 }
예제 #21
0
 /// <summary>
 /// Leave this room and returns to the hub.
 /// </summary>
 /// <param name="clientThread"></param>
 public void LeaveRoom(ClientThread clientThread)
 {
     server.JoinRoom(clientThread, this, "hub");
     SendToAllClientsInRoom(new Message(MessageTypes.JoinRoom, JsonConvert.SerializeObject(new RoomModel(this.Name, this.clients.Count))));
 }
예제 #22
0
 /// <summary>
 /// An attempt to guess the word.
 /// </summary>
 /// <param name="word"></param>
 /// <param name="client"></param>
 public void GuessWord(string word, ClientThread client)
 {
     this.gameHandler.GuessWord(word, client.Name);
 }
예제 #23
0
 void CheckHeartBeat(TcpClient heartbeat, ClientThread c)
 {
     Messaging.SendCommand("return GetCompDB()", c.GetClientSocket());
     string[] compDB = Messaging.RecieveComputerDetails(c.GetClientSocket());
     KnownClient kc = new KnownClient(compDB[0], compDB[2], compDB[1], "N/A", "N/A", compDB[3]);
     bool connected = true;
     do
     {
         try
         {
             if (heartbeat.Client.Poll(0, SelectMode.SelectRead))
             {
                 byte[] buff = new byte[1];
                 if (heartbeat.Client.Receive(buff, SocketFlags.Peek) == 0)
                 {
                     MessageBox.Show("Client left :: ");
                     connected = false;
                 }
             }
         }
         catch (Exception e)
         {
            // MessageBox.Show("Client left :: ");
             connected = false;
             objectListOffline.AddObject(kc);
             objectListView1.RemoveObject(c);
         }
        // if (heartbeat.GetStream().ReadByte() < 0 ) { MessageBox.Show("Gone"); }
     } while (connected);
 }
예제 #24
0
        private void GetComputerHardware(List<ClientThread> c)
        {
            UpdateProgressBar(true);
            SetHardwareText(new string[]{""});
            SetHardwareUsage(new string[] {"", "", "", ""});
            foreach (ClientThread client in c)
            {
                Messaging.SendCommand("return SendHardwareUsage();", client.GetClientSocket());

                string[] HardwareUsage = Messaging.RecieveComputerDetails(client.GetClientSocket());

                Messaging.SendCommand("return SendHardwareDetails();", client.GetClientSocket());
                SetHardwareText(Messaging.RecieveComputerDetails(client.GetClientSocket()));
                SetHardwareUsage(HardwareUsage);

                Messaging.SendCommand("return GetProcessDetails();", client.GetClientSocket());
                List<Process> ProcList = (List<Process>)Messaging.RecieveProcessDetails(client.GetClientSocket());

                SetProcessDataGrid(ProcList);

                Messaging.SendCommand("return GetServiceDetails();", client.GetClientSocket());
                List<Service> ServiceList = (List<Service>)Messaging.RecieveServiceDetails(client.GetClientSocket());

                SetServiceDataGrid(ServiceList);

                Messaging.SendCommand("return GetTaskDetails();", client.GetClientSocket());
                List<ScheduledTask> TaskList = (List<ScheduledTask>)Messaging.RecieveTaskDetails(client.GetClientSocket());

                SetTaskDataGrid(TaskList);

                selectedClient = client;
            }
            UpdateProgressBar(false);
        }
예제 #25
0
        public bool checkNewClient(ClientThread c)
        {
            string id = Messaging.SendNewCommand("return GetCompID()", c.GetClientSocket());

            bool isNew = db.CheckForNewComputer(id);

            return isNew;
        }
예제 #26
0
        private void AddClientToDatabase(ClientThread client)
        {
            Messaging.SendCommand("return GetProcessDetails();", client.GetClientSocket());
            List<Process> compProc = (List<Process>)Messaging.RecieveProcessDetails(client.GetClientSocket());

            List<string> procnames = new List<string>();

            foreach (Process proc in compProc)
            {
                procnames.Add(proc.GetName());
            }

            procnames = procnames.Distinct().ToList();

            db.AddProcesses(client.GetCompID().Trim(), procnames);

            Messaging.SendCommand("return GetSystemDB()", client.GetClientSocket());
            string[] sysDB = Messaging.RecieveComputerDetails(client.GetClientSocket());

            db.InsertIntoTable("SYSTEMCOMPONENTS", sysDB);

            Messaging.SendCommand("return GetCompDB()", client.GetClientSocket());
            string[] compDB = Messaging.RecieveComputerDetails(client.GetClientSocket());

            db.InsertIntoTable("COMPUTER", compDB);
        }
예제 #27
0
        public string SendFileToOtherClient(string source , int fileLength, string destination, ClientThread toClient )
        {
            //Send FTP file
            //Tell client to look for the file
            // Messaging.SendNewCommand("RemoteSendFTP('"+toClient.GetClientIP()+"',"+toClient.GetPort()+",'" + source + "');", this.GetClientSocket());
            //return toClient.RecieveFileFromOtherClient(destination, fileLength, this.GetClientIP(), this.GetPort());

            //Ok, this will be the new server. The server will send the file.
            //Server will tell the client to connect to it, and wait for the file to send

            Messaging.SendNewCommand("RemoteAcceptFTPFromClient('" + this.GetClientIP() + "', '" + destination + "'," + fileLength + ");", toClient.GetClientSocket());
            //this line tells the toClient that to connect to the ip address, the file will be this big and store it here
            Messaging.SendNewCommand("SendFTPToClient('"+source.Replace("\\", "\\\\")+"','"+this.GetClientIP()+"');", this.GetClientSocket());
            //Messaging.ClientFTPToClient(source, this.GetClientIP());
            return "Sent";
        }
예제 #28
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.
                }
            }
        }
예제 #29
0
        private void SendFolder(FTPDirectory ftp, ClientThread client, string pathToGo, FTP par)
        {
            if (Directory.Exists(ftp.Path))
            {
                ftp.GetChildren();
                foreach (FTPDirectory f in ftp.Children)
                {
                    SendFolder(f, client, pathToGo + "\\\\" + f.Name, par);
                }
            }
            else
            {

                Messaging.SendCommand("RemoteAcceptFTP(" + client.GetPort() + ",'" + pathToGo + "', " + new FileInfo(ftp.Path).Length + ");", client.GetClientSocket());
                //Messaging.RemoteAcceptFTP(client.GetClientSocket(), pathToGo);
                Messaging.FTPFile(ftp.Path, client.GetClientSocket());
                log.Text += ("\nFile successfully sent.\n");
                par.Refresh();
            }
        }
예제 #30
0
 /// <summary>
 /// Remove a client from the server and this room.
 /// </summary>
 /// <param name="clientThread"></param>
 public void DisconnectClient(ClientThread clientThread)
 {
     RemoveClient(clientThread);
 }
예제 #31
0
 /// <summary>
 /// Return the children directories of a remote directory, if any.
 /// </summary>
 /// <param name="node">The node is a GUID number, which points to the directory location on the remove machine.</param>
 /// <param name="client"></param>
 /// <returns></returns>
 private List<FTPDirectory> GetRemoteChildren(object node, ClientThread client)
 {
     this.client = client;
     Messaging.SendCommand("return GetFTPDirectory('" + ((FTPDirectory)node).GetID() + "');", client.GetClientSocket());
     FTPDirectory dir = (FTPDirectory)Messaging.RecieveFTPDirectory(client.GetClientSocket());
     return dir.Children;
 }
예제 #32
0
 /// <summary>
 /// Set the new drawer to a clientThread
 /// </summary>
 /// <param name="clientThread"></param>
 private void SetDrawer(ClientThread clientThread)
 {
     this.drawer = clientThread;
 }
예제 #33
0
 /// <summary>
 /// Make the entered clientthread the host.
 /// </summary>
 /// <param name="clientThread"></param>
 private void MakeHost(ClientThread clientThread)
 {
     this.host = clientThread;
     clientThread.SendMessage(new Message(MessageTypes.NewHost, JsonConvert.SerializeObject(new ClientModel(clientThread.Name, true))));
 }