与多个TCP客户端同时通信

时间:2019-01-26 01:26:07

标签: c# sockets server

我正在尝试创建一个工作跟踪器应用程序,我希望将其放置在雇主可以阻止该应用程序的位置,该应用程序立即在所有员工的计算机上被阻止。到目前为止,这是我为服务器提供的代码:

 private void btnStart_Click(object sender, EventArgs e)
        {
            // Start listening for TCP connections:
            listener = new TcpListener(IPAddress.Any, int.Parse(txtServerPort.Text));
            listener.Start();
            lbChat.Items.Add($"Server started on IP: {displayIP} and Port: {txtServerPort.Text}");
            isStarted = true;
            Server.RunWorkerAsync();
        }

        private void Server_DoWork(object sender, DoWorkEventArgs e)
        {
            while (true)
            {
                TcpClient client = listener.AcceptTcpClient();
                clientList.Add(client);
                foreach(TcpClient tcp in clientList)
                {
                    STR = new StreamReader(tcp.GetStream());
                    STW = new StreamWriter(tcp.GetStream());
                    STW.AutoFlush = true;
                }

                if(!backgroundWorker1.IsBusy & !backgroundWorker2.IsBusy)
                {
                    backgroundWorker1.RunWorkerAsync();
                    backgroundWorker2.WorkerSupportsCancellation = true;
                }
            }
        }

        // Recieve messages from the employee's computer:
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            foreach(TcpClient tcp in clientList)
            {
                while (tcp.Connected)
                {
                    try
                    {
                        recieve = STR.ReadLine();
                        this.lbChat.Invoke(new MethodInvoker(delegate ()
                        {
                            lbChat.Items.Add(recieve + "\n");
                            if (recieve.Contains(">"))
                            {
                                // Add employee name to connected list:
                                string name = recieve.Split(new string[] { "'" }, StringSplitOptions.None)[1].Split('\'')[0].Trim();
                                employeesList.Add(name);
                                lbConnected.Items.Add(name);
                            }
                            else if (recieve.Contains("<"))
                            {
                                string name = recieve.Split(new string[] { "'" }, StringSplitOptions.None)[1].Split('\'')[0].Trim();
                                employeesList.Remove(name);
                                lbConnected.Items.Clear();
                                foreach (string employee in employeesList)
                                {
                                    lbConnected.Items.Add(employee);
                                }
                            }
                        }));
                        recieve = "";
                    }
                    catch (Exception ex)
                    {
                        Console.Write(ex.Message.ToString());
                    }
                }
            }

        }

        // Send messages to employee's computer:
        private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
        {
            foreach(TcpClient tcp in clientList)
            {
                if (tcp.Connected)
                {
                    STW.WriteLine(TextToSend);
                }
                else
                {
                    MessageBox.Show("Sending failed");
                }
            }
        }

基本上,服务器将收到有关第一个客户端已连接的通知,但是它不会从连接的第二个客户端接收此通知,但是只有第二个客户端在连接后才接收和发送通知,而第一个客户则没有。但是,如果我先断开第一个客户端的连接,则服务器会收到有关第一个客户端已断开连接的通知,然后当第二个客户端断开连接时,它也会收到通知。但是,如果我先断开第二个客户端的连接,则不会通知服务器。所以我真的不确定这一切是什么。

0 个答案:

没有答案