Exemple #1
0
        void SetEvent(TcpClientX session)
        {
            session.Received += delegate(Object sender, NetEventArgs e)
            {
                TcpClientX tc = sender as TcpClientX;

                Stream stream = null;
                if (!tc.Items.Contains("Stream"))
                {
                    stream = new MemoryStream();
                    tc.Items["Stream"] = stream;
                }
                else
                {
                    stream = tc.Items["Stream"] as Stream;
                }
                
                // 把数据写入流
                e.WriteTo(stream);

                // 数据太少时等下一次,不过基本上不可能。5是FileFormat可能的最小长度
                if (stream.Length < 5) return;

                FileFormat format = FileFormat.Load(stream);
            };
            session.Error += new EventHandler<NetEventArgs>(OnError);
        }
Exemple #2
0
        /// <summary>创建会话</summary>
        /// <param name="e"></param>
        /// <returns></returns>
        protected virtual ISocketSession CreateSession(NetEventArgs e)
        {
            var session = new TcpClientX();

            session.Socket = e.AcceptSocket;
            //session.RemoteEndPoint = e.AcceptSocket.RemoteEndPoint as IPEndPoint;
            //if (e.RemoteEndPoint == null) e.RemoteEndPoint = session.RemoteEndPoint;
            session.SetRemote(e);
            // 对于服务器中的会话来说,收到空数据表示断开连接
            // session.DisconnectWhenEmptyData = true;

            return(session);
        }
Exemple #3
0
        static void MaxTest2(Object state)
        {
            Int32 n = (Int32)state;
            Int32 err = 0;

            for (int i = 0; i < 2000; i++)
            {
                TcpClientX client = new TcpClientX();
                SetEvent(client, false);
                try
                {
                    client.Connect(IPAddress.Loopback, 7);
                    //client.Connect("192.168.1.101", 7);
                    client.ReceiveAsync();

                    client.Send(String.Format("Hello {0} {1}", n, i));
                }
                catch //(Exception ex)
                {
                    //Console.WriteLine("i={0}", i);
                    //Console.WriteLine(ex.ToString());
                    err++;
                    //break;
                }
            }

            Thread.Sleep(10000);
            if (err > 0) Console.WriteLine("线程{0},失败{1}", n, err);
        }
Exemple #4
0
        static void SetEvent(TcpClientX session, Boolean isServer)
        {
            String name = isServer ? "服务端" + (session as TcpSession).ID : "客户端";
            session.Received += delegate(Object sender, NetEventArgs e)
            {
                //String address = e.AcceptSocket == null ? "" : e.AcceptSocket.RemoteEndPoint.ToString();
                //XTrace.WriteLine("{2} 收到{0}数据![{3}]{1}", e.RemoteEndPoint, e.GetString(), name, e.BytesTransferred);

                if (isServer)
                {
                    TcpClientX tc = sender as TcpClientX;
                    tc.Send("你的信息已收到!");
                }
            };
            session.Error += delegate(Object sender, NetEventArgs e)
            {
                //if (e.SocketError != SocketError.Success)
                //    XTrace.WriteLine("{2} {0}错误 {1}", e.RemoteEndPoint, e.SocketError, name);
                //else if (e.UserToken is Exception)
                //    XTrace.WriteLine("{2} {0}错误 {1}", e.RemoteEndPoint, e.UserToken, name);
                ////else
                ////    XTrace.WriteLine("{1} {0}断开!", e.RemoteEndPoint, name);
            };
        }