Пример #1
0
        /// <summary>
        /// 添加Socket
        /// </summary>
        /// <param name="socket">接收到的Socket</param>
        /// <param name="flag">该Socket类型</param>
        public void AddSocket(Socket socket, int flag)
        {
            lock (_sync)
            {
                ArmClient armClient = null;
                TcpPort   tempPort  = new TcpPort(socket);
                InstallClientPort(tempPort, flag, ref armClientDictionary);

                if (!(HasValDevice(out armClient)))
                {
                    return;
                }

                if (armClient != null)
                {
                    Console.WriteLine("ARM客户端开始接收数据");
                    TcpPort comPort  = armClient.ControlPort;
                    byte[]  location = new byte[200];
                    comPort.Receive(location);
                    armClient.Location = Encoding.ASCII.GetString(location);
                    Console.WriteLine("ARM客户端地址为:" + armClient.Location);


                    byte[] data = Encoding.ASCII.GetBytes("Welcome to server");
                    comPort.Send(data, Kind.message);
                    armClient.IsChecking     = false;
                    armClient.IsUsing        = false;
                    armClient.LastAccessTime = DateTime.Now;
                    armClient.IsLoseConnect  = false;
                }
            }

            //lock (_sync)
            //{
            //    if (controlListenning) //仍然在监听状态
            //    {
            //        lock (listSync) //套大锁,再套小锁
            //        {
            //            //检查该客户端之前是否已经登录过了。有的话,删除之前的,然后再添加

            //            int i;
            //            for (i = 0; i < clients.Count; ++i)
            //            {
            //                if (one_client.localtion.Equals(clients[i].localtion))
            //                {
            //                    clients.Remove(clients[i]);
            //                    break;
            //                }
            //            }

            //            clients.Add(one_client); //把连接好的客户端放到容器里面
            //            ShowClientsinGrid(one_client);
            //            lb_conn_num.Text = "" + clients.Count;
            //        }
            //    }
            //}
        }
Пример #2
0
 /// <summary>
 ///  armClientDictionary
 /// </summary>
 ///
 /// <param name="socket"></param>
 /// <param name="flag"></param>
 public void AddClient(Socket socket, int flag)
 {
     lock (deviceSync)
     {
         ArmClient armClient = null;
         TcpPort   tempPort  = new TcpPort(socket);
         AddClientPort(tempPort, flag, ref armClientDictionary);
         if (!(HasValDevice(out armClient)))
         {
             return;
         }
         if (armClient != null)
         {
             TcpPort comPort   = armClient.ControlPort;
             byte[]  nodeIdMes = new byte[200];
             comPort.Receive(nodeIdMes);
             armClient.Id = (Encoding.ASCII.GetString(nodeIdMes)).TrimEnd('\0');
             Console.WriteLine("客户端编号为" + armClient.Id);
             byte[] data = Encoding.ASCII.GetBytes("Welcome to server");
             comPort.Send(data, Kind.message);
             NodeBLL nodeBll = new NodeBLL();
             int     clientId;
             if (Int32.TryParse(armClient.Id, out clientId))
             {
                 UpdateArmClientId(armClient.Ipaddress, clientId);
                 NodeData nodeData = nodeBll.GetDetailByPK(clientId);
                 if (nodeData != null)
                 {
                     armClient.Location = nodeData.location;
                     if (controlListenning)
                     {
                         AddClientToList(armClient, ref armClientList);
                     }
                     DeleteClientPort(armClient.Ipaddress, ref armClientDictionary);
                     armClient.IsChecking     = false;
                     armClient.IsUsing        = false;
                     armClient.LastAccessTime = DateTime.Now;
                     armClient.IsLoseConnect  = false;
                     armClient.Status         = true;
                 }
             }
         }
     }
 }