Пример #1
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="_tmpTcpClient">傳入TcpClient參數</param>
 public HandleClient(PacketSlice slice, TcpClient _tmpTcpClient, Timer timer)
 {
     this.dbmodel    = new DataBaseUnit();
     this.mTcpClient = _tmpTcpClient;
     this.pslicer    = slice;
     this.timer      = timer;
 }
Пример #2
0
        }     // end Class
        /// <summary>
        /// 等待客戶端連線
        /// </summary>
        public void ListenToConnection()
        {
            DataBaseUnit dataBaseModel = new DataBaseUnit();
            PacketSlice  packetSlicer  = new PacketSlice();

            packetSlicer.useDefaultDict();

            //取得本機名稱
            string hostName = Dns.GetHostName();

            Console.WriteLine("本機名稱=" + hostName);

            //取得本機IP
            //IPAddress[] ipa = Dns.GetHostAddresses(hostName);
            //Console.WriteLine("本機IP=" + ipa[0].ToString());
            IPAddress ip = new IPAddress(new byte[] { 127, 0, 0, 1 });

            //建立本機端的IPEndPoint物件
            IPEndPoint ipe = new IPEndPoint(ip, 1234);

            //建立TcpListener物件
            TcpListener tcpListener = new TcpListener(ipe);

            //開始監聽port
            tcpListener.Start();
            Console.WriteLine("等待客戶端連線中... \n");

            TcpClient tmpTcpClient;
            int       numberOfClients = 0;

            while (true)
            {
                try
                {
                    //建立與客戶端的連線
                    tmpTcpClient = tcpListener.AcceptTcpClient();

                    if (tmpTcpClient.Connected)
                    {
                        Console.WriteLine("連線成功!");
                        HandleClient handleClient = new HandleClient(packetSlicer, tmpTcpClient, mainTimer);
                        Thread       myThread     = new Thread(new ThreadStart(handleClient.Communicate));
                        numberOfClients      += 1;
                        myThread.IsBackground = true;
                        myThread.Start();
                        myThread.Name = tmpTcpClient.Client.RemoteEndPoint.ToString();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Console.Read();
                }
            } // end while
        }     // end ListenToConnect()
Пример #3
0
 public TransModule(TcpClient client, PacketSlice slice)
 {
     this.client = client;
     this.stream = client.GetStream();
     this.slicer = slice;
 }