示例#1
0
        private void TCPClientListenerThread()
        {
            while (mCommState.IsOpen)
            {
                try
                {
                    //建立與客戶端的連線
                    TcpClient tcpClient = mTcpListener.AcceptTcpClient();

                    if (tcpClient.Connected)
                    {
                        EndPoint remoteEndPoint = tcpClient.Client.RemoteEndPoint;
                        string   address        = ((IPEndPoint)remoteEndPoint).Address.ToString();
                        int      port           = ((IPEndPoint)remoteEndPoint).Port;
                        string   key            = string.Format("{0}:{1}", address, port);
                        if (mCommTCPClientList.ContainsKey(key))
                        {
                            mCommTCPClientList[key].DisconnectEvent   -= Client_DisconnectEvent;
                            mCommTCPClientList[key].DataReceivedEvent -= Client_DataReceivedEvent;
                            mCommTCPClientList[key].Disconnect();
                            mCommTCPClientList.Remove(key);
                        }
                        CommTCPClient client = new CommTCPClient(tcpClient);
                        client.DisconnectEvent   += Client_DisconnectEvent;
                        client.DataReceivedEvent += Client_DataReceivedEvent;
                        mCommTCPClientList.Add(key, client);

                        DeviceConnect(address, port, DateTime.Now);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
示例#2
0
 public int Send(byte[] data, CommTCPClient tcpClient)
 {
     return(tcpClient.Send(data));
 }