예제 #1
0
            private void client_Received(object sender, TransEventArgs Data)
            {
                Console.WriteLine("client_Received:{0}", Data.ToString());
                DataTansService o = (DataTansService)sender;

                if (Data.TransType == EnumTransDataType.校验)
                {
                    if (MustVerify)
                    {
                        //校验不合格 从cliendTables中删除.
                        if (Verify(o.Key, Data.ToString()))
                        {
                            o.Available = true;
                            o.SendData(EnumTransDataType.欢迎, SERVICESWELLCOME);
                        }
                        else
                        {
                            clientTables.Remove(o.Key);
                        }
                    }
                    else
                    {
                        o.Available = true;
                    }
                }
                else if (Data.TransType != EnumTransDataType.未知 && o.Available)
                {
                    if (Data.TransType == EnumTransDataType.请求 && Data.ToString().Equals("closed", StringComparison.OrdinalIgnoreCase))
                    {
                        Console.WriteLine("Remove:{0}", o.Key);
                        o.Close();
                        if (Log != null)
                        {
                            Log.Write("Remove:" + o.Key);
                        }
                        clientTables.Remove(o.Key);
                    }
                    else
                    {
                        if (TransData != null)
                        {
                            TransData(sender, Data);
                        }
                    }
                }
            }
예제 #2
0
            private void DoListen()
            {
                try
                {
                    if (listener == null)
                    {
                        listener = new TcpListener(ip, port);
                    }
                    listener.Start();
                    while (true)
                    {
                        if (listener == null)
                        {
                            break;
                        }
                        if (listener.Pending())
                        {
                            _enRefresh = false;
                            DataTansService client = new DataTansService();
                            client.Key = Guid.NewGuid().ToString();
                            TcpClient tcpClient = listener.AcceptTcpClient();
                            if (client.CreateLink(tcpClient))
                            {
                                clientTables.Add(client.Key, client);
                                if (!MustVerify)
                                {
                                    client.Available = true;
                                }
                                Console.WriteLine("try Connect...{0},Count:{1}", client.Key, clientTables.Count.ToString());
                                clientTables[client.Key].Received += new TransEventHandler(client_Received);
                                Console.WriteLine("try Connect1");

                                ////StreamWriter streamWriter = new StreamWriter(clientTables[client.Key].tcpClient.GetStream());
                                ////Console.WriteLine("try Connect2");
                                //byte[] buff = Encoding.Default.GetBytes(client.Key);
                                //byte[] charBuff = new byte[buff.Length + 2];
                                //charBuff[0] = (byte)(TransTypeToChar(EnumTransDataType.校验));
                                ////char[] charBuff = new char[buff.Length * 2];
                                ////int i = Convert.ToBase64CharArray(buff, 0, buff.GetLength(0), charBuff, 1, Base64FormattingOptions.None);
                                ////charBuff[0] = TransTypeToChar(EnumTransDataType.校验);
                                //Array.Copy(buff, 0, charBuff, 1, buff.Length);
                                //charBuff[charBuff.Length - 1] = (byte)'.';//添加结尾标记
                                ////Array.Resize<char>(ref charBuff, i + 2);
                                //Console.WriteLine("try Connect2");
                                ////streamWriter.Write(charBuff);
                                //clientTables[client.Key].tcpClient.GetStream().Write(charBuff, 0, charBuff.Length);
                                //Console.WriteLine("try Connect3");
                                ////streamWriter.Flush();
                                ////Console.WriteLine("try Connect5");
                                //clientTables[client.Key].SendData(EnumTransDataType.校验, client.Key);
                            }
                            else
                            {
                                Console.WriteLine("CreateLink fail.{0},Count:{1}", client.Key, clientTables.Count.ToString());
                            }
                            _enRefresh = true;
                        }
                        listenerThread.Join(100);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    if (Log != null)
                    {
                        Log.Write(ex.Message);
                    }
                }
            }