NetServer.OnReceive改为INetSession触发
nnhy authored at 2016-07-21 11:21:23
1.31 KiB
X
using System;
using System.IO;
using NewLife.Net.Sockets;

namespace NewLife.Net.Application
{
    /// <summary>Echo服务。把客户端发来的数据原样返回。</summary>
    public class EchoServer : NetServer
    {
        /// <summary>实例化一个Echo服务</summary>
        public EchoServer()
        {
            // 默认7端口
            Port = 7;

            Name = "Echo服务";
        }

        /// <summary>已重载。</summary>
        /// <param name="session"></param>
        /// <param name="stream"></param>
        protected override void OnReceive(INetSession session, Stream stream)
        {
            if (stream.Length == 0) return;

            var p = stream.Position;
            if (stream.Length > 100)
                WriteLog("Echo {0} [{1}]", session.Remote, stream.Length);
            else
                WriteLog("Echo {0} [{1}] {2}", session.Remote, stream.Length, stream.ToStr());

            //Send(e.Socket, e.Buffer, e.Offset, stream.Length, e.RemoteEndPoint);
            //session.Send(e.Buffer, e.Offset, stream.Length, e.RemoteEndPoint);
            //session.Send(e.Buffer, e.Offset, stream.Length);
            stream.Position = p;
            //session.Send(stream);
            session.Send(stream.ReadBytes());
        }
    }
}