Ejemplo n.º 1
0
        /// <summary>
        /// 1、接收到客户端的连接,用 socket 对像的 Accept() 方
        ///    法创建一个新的用于和客户端进行通信的 socket 对像clientSocket
        /// 2、将socket对像clientSocket加入集合listSocket中
        /// 3、更新listview将新客户加入显示
        /// 4、更新连接数label显示信息,对话框添加连接信息
        /// 5、通过新的socket向新连接发送打招呼信息
        /// 6、创建新线程实时接受新客户socket发来的信息并显示到对话框中
        /// </summary>
        private void ListeningClientConnect()
        {
            for (; ;)
            {
                this.clientSocket = this.serverSocket.Accept();



                clientSocket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
                socketUser user = new socketUser(clientSocket);
                this.listSocket.Add(user);
                //this.listSocket.Add(this.clientSocket);



                this.stateInfo.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => this.stateInfo.Content = this.listSocket.Count + " 个连接."));
                //this.clientSocket.Send(Encoding.UTF8.GetBytes("你好,我是服务器!"));
                user.bw.Write("你好,我是服务器!来自CHARBW");
                new Thread(new ParameterizedThreadStart(this.ReceiveMessage))
                {
                    IsBackground = true
                }.Start(user);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 与某一客户socket通信的线程方法
        /// 1、将receive方法接收到的信息存储到缓冲数组message中
        /// 2、对字节信息进行解码并显示到相应textbox对话框中
        /// 3、如果客户端断开,则receive方法抛出异常,并将相应
        ///    异常信息更新显示到textbox和状态label中
        /// </summary>
        /// <param name="clientSocket"></param>
        private void ReceiveMessage(object newAcceptedUser)
        {
            socketUser user = (socketUser)newAcceptedUser;

            //Socket socket = (Socket)clientSocket;
            for (; ;)
            {
                try
                {
                    //int count = socket.Receive(MainWindow.message);
                    string message          = user.br.ReadString();
                    string collecorEndPoint = Convert.ToString(user.remoteClient.RemoteEndPoint.ToString());
                    //string message = Encoding.UTF8.GetString(MainWindow.message, 0, count);
                    string[] messageInfo = message.Split(new char[] { '|' });
                    if (messageInfo.Length > 1)
                    {
                        switch (messageInfo[0])
                        {
                        case "iWantCollect":
                            txtmsg.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => txtmsg.Text += messageInfo[1] + "(" + collecorEndPoint + ")向该主控发起采集确认\r\n"));
                            txtmsg.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => txtmsg.Text += "主控设别向三台采集设备发起采集命令\r\n"));
                            SendCollectOrder("doCollect");
                            break;

                        case "SendFile":
                            string currentCollector = messageInfo[3];
                            filename = currentCollector + Path.GetFileName(messageInfo[1]);
                            length   = Convert.ToInt32(messageInfo[2]);
                            txtmsg.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => txtmsg.Text += "提前接收文件描述信息:来自[" + currentCollector + "],文件名[" + filename + "],大小[" + length + "]字节" + "\r\n"));
                            break;

                        case "Login":
                            this.L_ClientList.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => this.L_ClientList.Items.Add(Convert.ToString("[" + messageInfo[2] + "]:" + messageInfo[1]))));
                            this.txtmsg.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => txtmsg.Text = this.listSocket.Count + " 个连接来自[" + messageInfo[2] + "]:" + messageInfo[1] + "\r\n"));
                            break;

                        case "iWantCollectFace":
                            txtmsg.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => txtmsg.Text += messageInfo[1] + "(" + collecorEndPoint + ")向该主控发起抓脸采集确认\r\n"));
                            txtmsg.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => txtmsg.Text += "主控设别向三台采集设备发起抓脸采集命令\r\n"));
                            SendCollectOrder("doCollectFace");
                            break;

                        default:
                            break;
                        }
                    }
                    else if (message != "")
                    {
                        this.txtmsg.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => txtmsg.Text += "未知格式消息:" + message + "hhh"));
                        //txtmsg.Text += "未知格式消息:" + message;
                    }
                }
                catch (Exception ex)
                {
                    this.txtmsg.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => txtmsg.Text += user.remoteClient.ToString()));
                    this.txtmsg.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => txtmsg.Text += " 断开连接...\r\n" + ex.ToString()));
                    this.listSocket.Remove(user);

                    for (int i = 0; i < L_ClientList.Items.Count; i++)
                    {
                        string str = "";
                        this.L_ClientList.Dispatcher.Invoke(new Action(() => { str = L_ClientList.Items[i].ToString(); }));
                        if (str.Contains(user.remoteClient.ToString()))
                        {
                            this.L_ClientList.Dispatcher.Invoke(new Action(() => { L_ClientList.Items.RemoveAt(i); }));
                        }
                    }
                    this.stateInfo.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => this.stateInfo.Content = this.listSocket.Count + " 个连接."));
                    //socket.Shutdown(SocketShutdown.Both);
                    //socket.Close();
                    user.Close();
                    break;
                }
            }
        }