public override void ChannelRead(IChannelHandlerContext context, object message)
        {
            var byteBuffer = message as IByteBuffer;

            if (byteBuffer != null)
            {
                if (isInit)
                {
                    IPEndPoint iPEndPoint = context.Channel.LocalAddress as IPEndPoint;
                    if (iPEndPoint != null)
                    {
                        this.localIP   = iPEndPoint.Address.ToString();
                        this.localPort = iPEndPoint.Port;
                    }
                    iPEndPoint = context.Channel.RemoteAddress as IPEndPoint;
                    if (iPEndPoint != null)
                    {
                        this.remoteIP   = iPEndPoint.Address.ToString();
                        this.remotePort = iPEndPoint.Port;
                    }
                    isInit = false;
                }
                NetSocket netSocket = new NetSocket()
                {
                    data = byteBuffer.Array, LocalHost = localIP, LocalPort = localPort, RemoteHost = remoteIP, RemotePort = remotePort
                };
                this.nettyClient.AddData(netSocket);
            }
        }
Example #2
0
 public void AddData(NetSocket netSocket)
 {
     if (recviceData != null)
     {
         recviceData(netSocket);
     }
     else
     {
         netSockets.Add(netSocket);
     }
 }
Example #3
0
        //  重写基类的方法,当消息到达时触发,这里收到消息后,在控制台输出收到的内容,并原样返回了客户端
        public override void ChannelRead(IChannelHandlerContext context, object message)
        {
            var buffer = message as IByteBuffer;

            if (buffer != null)
            {
                // Console.WriteLine("Received from client: " + buffer.ToString(Encoding.UTF8));
                if (isInit)
                {
                    IPEndPoint iPEndPoint = context.Channel.LocalAddress as IPEndPoint;
                    if (iPEndPoint != null)
                    {
                        this.localIP   = iPEndPoint.Address.ToString();
                        this.localPort = iPEndPoint.Port;
                    }
                    isInit = false;
                }
                if (heart == null)
                {
                    heart = NettyConfig.NettyEncod.GetBytes(NettyConfig.HeartMessage);
                }
                if (buffer.Array.Length == heart.Length)
                {
                    if (NettyConfig.NettyEncod.GetString(buffer.Array) == NettyConfig.HeartMessage)
                    {
                        return;
                    }
                }
                IPEndPoint endPoint  = context.Channel.LocalAddress as IPEndPoint;
                NetSocket  netSocket = new NetSocket()
                {
                    data = buffer.Array, LocalHost = localIP, LocalPort = localPort, RemoteHost = endPoint.Address.ToString(), RemotePort = endPoint.Port, channel = context.Channel
                };
                nettyServer.AddData(netSocket);
            }
            // context.WriteAsync(message);//写入输出流
        }