Пример #1
0
 /// <summary>
 /// 已重载。
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 protected override void OnReceived(object sender, NetEventArgs e)
 {
     if (e.BytesTransferred > 100)
         WriteLog("Chargen {0} [{1}]", e.RemoteEndPoint, e.BytesTransferred);
     else
         WriteLog("Chargen {0} [{1}] {2}", e.RemoteEndPoint, e.BytesTransferred, e.GetString());
 }
Пример #2
0
 /// <summary>
 /// 已重载。
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 protected override void OnReceived(object sender, NetEventArgs e)
 {
     try
     {
         if (e.BytesTransferred > 100)
             WriteLog("Discard {0} [{1}]", e.RemoteEndPoint, e.BytesTransferred);
         else
             WriteLog("Discard {0} [{1}] {2}", e.RemoteEndPoint, e.BytesTransferred, e.GetString());
     }
     finally
     {
         Disconnect(e.UserToken as SocketBase);
     }
 }
Пример #3
0
        /// <summary>
        /// 已重载。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected override void OnReceived(object sender, NetEventArgs e)
        {
            try
            {
                if (e.BytesTransferred > 100)
                    WriteLog("Echo {0} [{1}]", e.RemoteEndPoint, e.BytesTransferred);
                else
                    WriteLog("Echo {0} [{1}] {2}", e.RemoteEndPoint, e.BytesTransferred, e.GetString());

                Send(e.UserToken as SocketBase, e.Buffer, e.Offset, e.BytesTransferred, e.RemoteEndPoint);

                // 等一秒,等客户端接收数据
                Thread.Sleep(1000);
            }
            finally
            {
                Disconnect(e.UserToken as SocketBase);
            }
        }
Пример #4
0
        /// <summary>
        /// 已重载。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected override void OnReceived(object sender, NetEventArgs e)
        {
            if (e.BytesTransferred > 1024)
            {
                WriteLog("{0}的数据包大于1k,抛弃!", e.RemoteEndPoint);
                return;
            }

            //WriteLog("{0} {1}", e.RemoteEndPoint, Encoding.UTF8.GetString(e.Buffer, e.Offset, e.BytesTransferred));
            WriteLog("{0} [{1}] {2}", e.RemoteEndPoint, e.BytesTransferred, e.GetString());

            // 兼容IPV6
            IPEndPoint remote = e.RemoteEndPoint as IPEndPoint;
            if (remote != null && remote.Address != IPAddress.Any && remote.Address != IPAddress.IPv6Any)
            {
                UdpServer us = sender as UdpServer;
                us.Send(e.Buffer, e.Offset, e.BytesTransferred, e.RemoteEndPoint);
                // 这里发送完成后不需要关闭Socket,因为这是UdpServer的Socket
            }
        }
Пример #5
0
        /// <summary>
        /// 已重载。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected override void OnReceived(object sender, NetEventArgs e)
        {
            TcpClientX tc = sender as TcpClientX;

            try
            {
                if (e.BytesTransferred > 1024)
                {
                    WriteLog("{0}的数据包大于1k,抛弃!", tc.RemoteEndPoint);
                }
                else
                {
                    WriteLog("{0} [{1}] {2}", tc.RemoteEndPoint, e.BytesTransferred, e.GetString());

                    if (tc != null && tc.Client.Connected) tc.Send(e.Buffer, e.Offset, e.BytesTransferred);
                }
            }
            finally
            {
                tc.Close();
            }
        }
Пример #6
0
        static void server_Accepted(object sender, NetEventArgs e)
        {
            TcpSession session = e.UserToken as TcpSession;
            if (session == null) return;

            //if (e.AcceptSocket != null)
            //    Console.WriteLine("{1} 新连接 {0}", e.AcceptSocket.RemoteEndPoint, session.ID);
            //else
            if (e.AcceptSocket == null)
                Console.WriteLine("奇怪");

            //SetEvent(session, true);

            //XTrace.WriteLine(e.GetString());
            //session.Send("欢迎!");
            session.Send(e.GetString());

            session.Push(e);
            session.Close();
        }