Ejemplo n.º 1
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;
                }
            }
        }