`
liuzhengqiu0127
  • 浏览: 7236 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

Netty应用基础篇

阅读更多

本篇主要介绍Netty解决TCP粘包和拆包问题。

 

概念介绍

TCP是个”流"协议,所谓流,就是没有界限的一串数据。TCP底层并不了解上层业务数据的具体含义,它会根据TCP缓冲区的实际情况进行包的划分,所以在业务上认为,一个完整的包可能会被TCP拆分成多个包进行发送,也有可能把多个小的包封装成一个大的数据包发送,这就是所谓的TCP粘包和拆包问题。

产生原因:

1,应用程序write写入的字节大小大于套接口发送缓冲区大小;

2,进行MSS大小的TCP分段;

3,以太网帧的payload大于MTU进行IP分片。

 

实例如下

 

服务端代码:

 

package com.huawei.netty.test;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * Created by liuzhengqiu on 2017/10/15.
 */
public class NettyServer
{
    public void bind(int port) throws Exception
    {
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try
        {
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(bossGroup,workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .option(ChannelOption.SO_BACKLOG,1024)
                    .childHandler(new ChildChannelHandler());
            ChannelFuture channelFuture = serverBootstrap.bind(port).sync();
            channelFuture.channel().closeFuture().sync();
        }
        finally
        {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }

    private class ChildChannelHandler extends ChannelInitializer<SocketChannel>
    {

        @Override
        protected void initChannel(SocketChannel socketChannel) throws Exception
        {
            socketChannel.pipeline().addLast(new NettyServerHandler());
        }
    }

    public static void main(String[] args) throws Exception {
        new NettyServer().bind(8080);
    }
}

 

 

package com.huawei.netty.test;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.Date;

/**
 * Created by liuzhengqiu on 2017/10/15.
 */
public class NettyServerHandler extends ChannelInboundHandlerAdapter
{
    private int counter;

    @Override
    public void channelRead(ChannelHandlerContext ctx,Object msg) throws Exception
    {
        ByteBuf byteBuf = (ByteBuf) msg;
        byte[] req = new byte[byteBuf.readableBytes()];
        byteBuf.readBytes(req);
        String body = new String(req,"UTF-8");
        System.out.println("The time server receive order :" + body
            + ";the counter is :"+ ++counter);

        String currentTime = "hello world".equalsIgnoreCase(body)?new Date().toString():"bad order";
        currentTime += System.getProperty("line.separator");
        ByteBuf resp = Unpooled.copiedBuffer(currentTime.getBytes());
        ctx.write(resp);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception
    {
        ctx.flush();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx,Throwable cause)
    {
        ctx.close();
    }
}

 

客户端代码如下:

 

package com.huawei.netty.test;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

/**
 * Created by liuzhengqiu on 2017/10/15.
 */
public class NettyClient
{
    public void connect(int port,String host) throws Exception
    {
        EventLoopGroup group = new NioEventLoopGroup();
        try
        {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(group).channel(NioSocketChannel.class)
                    .option(ChannelOption.TCP_NODELAY,true)
                    .handler(new ChannelInitializer<SocketChannel>() {

                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            socketChannel.pipeline().addLast(new NettyClientHandler());
                        }
                    });
            ChannelFuture f = bootstrap.connect(host,port).sync();
            f.channel().closeFuture().sync();
        }
        finally {
            group.shutdownGracefully();
        }
    }

    public static void main(String[] args) throws Exception {
        new NettyClient().connect(8080,"127.0.0.1");
    }
}

 

 

package com.huawei.netty.test;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * Created by liuzhengqiu on 2017/10/15.
 */
public class NettyClientHandler extends ChannelInboundHandlerAdapter
{
    private int counter;

    private final ByteBuf msg;

    public NettyClientHandler()
    {
        byte[] req = ("hello world"+System.getProperty("line.separator")).getBytes();
        msg = Unpooled.buffer(req.length);
        msg.writeBytes(req);
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx)
    {
        ByteBuf message = null;
        byte[] req = ("hello world"+System.getProperty("line.separator")).getBytes();
        for (int i=0;i<100;i++)
        {
            message = Unpooled.buffer(req.length);
            message.writeBytes(req);
            ctx.writeAndFlush(message);
        }
        ctx.writeAndFlush(msg);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx,Object msg) throws Exception
    {
        ByteBuf buf = (ByteBuf) msg;
        byte[] req = new byte[buf.readableBytes()];
        buf.readBytes(req);

        String body = new String(req,"UTF-8");
        System.out.println("Now is :"+body);
    }
}

 

服务端运行结果:

 

The time server receive order :hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello worl;the counter is :1
The time server receive order :d
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
;the counter is :2

 

客户端运行结果如下:

 

Now is :bad order
bad order

 

Netty的解决方法

利用LineBasedFrameDecoder和StringDecoder解决TCP粘包问题。

LineBasedFrameDecoder原理:依次遍历ByteBuf中的可读字节,判断看是否有”\n"或者“\r\n",如果有,就以此位置为结束位置,从可读索引到结束位置区间的字节就组成了一行。它是以换行符为结束标志的解码器,支持携带结束符或者不携带结束符两种解码方式,同时支持配置单行的最大长度。如果连续读取到最大长度后仍然没有发现换行符,就会抛出异常。

StringDecoder:就是将收到的对象转换成字符串。

 

Netty还支持DelimiterBasedFrameDecoder和FixedLengthFrameDecoder。

DelimiterBasedFrameDecoder:用于对使用分隔符结尾的消息进行自动解码。

FixedLengthFrameDecoder:用于对固定长度的消息进行自动解码。

 

Netty解决方法实例

 

服务端:

package com.huawei.netty.test;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;

/**
 * Created by liuzhengqiu on 2017/10/15.
 */
public class NettyServer
{
    public void bind(int port) throws Exception
    {
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try
        {
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(bossGroup,workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .option(ChannelOption.SO_BACKLOG,1024)
                    .childHandler(new ChildChannelHandler());
            ChannelFuture channelFuture = serverBootstrap.bind(port).sync();
            channelFuture.channel().closeFuture().sync();
        }
        finally
        {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }

    private class ChildChannelHandler extends ChannelInitializer<SocketChannel>
    {

        @Override
        protected void initChannel(SocketChannel socketChannel) throws Exception
        {
            socketChannel.pipeline()
                    .addLast(new LineBasedFrameDecoder(1024))
                    .addLast(new StringDecoder())
                    .addLast(new NettyServerHandler());
        }
    }

    public static void main(String[] args) throws Exception {
        new NettyServer().bind(8080);
    }
}

 

package com.huawei.netty.test;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.Date;

/**
 * Created by liuzhengqiu on 2017/10/15.
 */
public class NettyServerHandler extends ChannelInboundHandlerAdapter
{
    private int counter;

    @Override
    public void channelRead(ChannelHandlerContext ctx,Object msg) throws Exception
    {
//        ByteBuf byteBuf = (ByteBuf) msg;
//        byte[] req = new byte[byteBuf.readableBytes()];
//        byteBuf.readBytes(req);
//        String body = new String(req,"UTF-8");
        String body = (String) msg;
        System.out.println("The time server receive order :" + body
            + ";the counter is :"+ ++counter);

        String currentTime = "hello world".equalsIgnoreCase(body)?new Date().toString():"bad order";
        currentTime += System.getProperty("line.separator");
        ByteBuf resp = Unpooled.copiedBuffer(currentTime.getBytes());
        ctx.write(resp);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception
    {
        ctx.flush();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx,Throwable cause)
    {
        ctx.close();
    }
}

 

客户端:

 

package com.huawei.netty.test;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;

/**
 * Created by liuzhengqiu on 2017/10/15.
 */
public class NettyClient
{
    public void connect(int port,String host) throws Exception
    {
        EventLoopGroup group = new NioEventLoopGroup();
        try
        {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(group).channel(NioSocketChannel.class)
                    .option(ChannelOption.TCP_NODELAY,true)
                    .handler(new ChannelInitializer<SocketChannel>() {

                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            socketChannel.pipeline()
                                    .addLast(new LineBasedFrameDecoder(1024))
                                    .addLast(new StringDecoder())
                                    .addLast(new NettyClientHandler());
                        }
                    });
            ChannelFuture f = bootstrap.connect(host,port).sync();
            f.channel().closeFuture().sync();
        }
        finally {
            group.shutdownGracefully();
        }
    }

    public static void main(String[] args) throws Exception {
        new NettyClient().connect(8080,"127.0.0.1");
    }
}

 

package com.huawei.netty.test;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * Created by liuzhengqiu on 2017/10/15.
 */
public class NettyClientHandler extends ChannelInboundHandlerAdapter
{
    private int counter;

    private final ByteBuf msg;

    public NettyClientHandler()
    {
        byte[] req = ("hello world"+System.getProperty("line.separator")).getBytes();
        msg = Unpooled.buffer(req.length);
        msg.writeBytes(req);
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx)
    {
        ByteBuf message = null;
        byte[] req = ("hello world"+System.getProperty("line.separator")).getBytes();
        for (int i=0;i<100;i++)
        {
            message = Unpooled.buffer(req.length);
            message.writeBytes(req);
            ctx.writeAndFlush(message);
        }
        ctx.writeAndFlush(msg);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx,Object msg) throws Exception
    {
//        ByteBuf buf = (ByteBuf) msg;
//        byte[] req = new byte[buf.readableBytes()];
//        buf.readBytes(req);
//
//        String body = new String(req,"UTF-8");
        String body = (String) msg;
        System.out.println("Now is :"+body);
    }
}

 

服务端运行结果:

The time server receive order :hello world;the counter is :1
The time server receive order :hello world;the counter is :2
The time server receive order :hello world;the counter is :3
The time server receive order :hello world;the counter is :4
The time server receive order :hello world;the counter is :5
The time server receive order :hello world;the counter is :6
The time server receive order :hello world;the counter is :7
The time server receive order :hello world;the counter is :8
The time server receive order :hello world;the counter is :9
The time server receive order :hello world;the counter is :10
The time server receive order :hello world;the counter is :11
The time server receive order :hello world;the counter is :12
The time server receive order :hello world;the counter is :13
The time server receive order :hello world;the counter is :14
The time server receive order :hello world;the counter is :15
The time server receive order :hello world;the counter is :16
The time server receive order :hello world;the counter is :17
The time server receive order :hello world;the counter is :18
The time server receive order :hello world;the counter is :19
The time server receive order :hello world;the counter is :20
The time server receive order :hello world;the counter is :21
The time server receive order :hello world;the counter is :22
The time server receive order :hello world;the counter is :23
The time server receive order :hello world;the counter is :24
The time server receive order :hello world;the counter is :25
The time server receive order :hello world;the counter is :26
The time server receive order :hello world;the counter is :27
The time server receive order :hello world;the counter is :28
The time server receive order :hello world;the counter is :29
The time server receive order :hello world;the counter is :30
The time server receive order :hello world;the counter is :31
The time server receive order :hello world;the counter is :32
The time server receive order :hello world;the counter is :33
The time server receive order :hello world;the counter is :34
The time server receive order :hello world;the counter is :35
The time server receive order :hello world;the counter is :36
The time server receive order :hello world;the counter is :37
The time server receive order :hello world;the counter is :38
The time server receive order :hello world;the counter is :39
The time server receive order :hello world;the counter is :40
The time server receive order :hello world;the counter is :41
The time server receive order :hello world;the counter is :42
The time server receive order :hello world;the counter is :43
The time server receive order :hello world;the counter is :44
The time server receive order :hello world;the counter is :45
The time server receive order :hello world;the counter is :46
The time server receive order :hello world;the counter is :47
The time server receive order :hello world;the counter is :48
The time server receive order :hello world;the counter is :49
The time server receive order :hello world;the counter is :50
The time server receive order :hello world;the counter is :51
The time server receive order :hello world;the counter is :52
The time server receive order :hello world;the counter is :53
The time server receive order :hello world;the counter is :54
The time server receive order :hello world;the counter is :55
The time server receive order :hello world;the counter is :56
The time server receive order :hello world;the counter is :57
The time server receive order :hello world;the counter is :58
The time server receive order :hello world;the counter is :59
The time server receive order :hello world;the counter is :60
The time server receive order :hello world;the counter is :61
The time server receive order :hello world;the counter is :62
The time server receive order :hello world;the counter is :63
The time server receive order :hello world;the counter is :64
The time server receive order :hello world;the counter is :65
The time server receive order :hello world;the counter is :66
The time server receive order :hello world;the counter is :67
The time server receive order :hello world;the counter is :68
The time server receive order :hello world;the counter is :69
The time server receive order :hello world;the counter is :70
The time server receive order :hello world;the counter is :71
The time server receive order :hello world;the counter is :72
The time server receive order :hello world;the counter is :73
The time server receive order :hello world;the counter is :74
The time server receive order :hello world;the counter is :75
The time server receive order :hello world;the counter is :76
The time server receive order :hello world;the counter is :77
The time server receive order :hello world;the counter is :78
The time server receive order :hello world;the counter is :79
The time server receive order :hello world;the counter is :80
The time server receive order :hello world;the counter is :81
The time server receive order :hello world;the counter is :82
The time server receive order :hello world;the counter is :83
The time server receive order :hello world;the counter is :84
The time server receive order :hello world;the counter is :85
The time server receive order :hello world;the counter is :86
The time server receive order :hello world;the counter is :87
The time server receive order :hello world;the counter is :88
The time server receive order :hello world;the counter is :89
The time server receive order :hello world;the counter is :90
The time server receive order :hello world;the counter is :91
The time server receive order :hello world;the counter is :92
The time server receive order :hello world;the counter is :93
The time server receive order :hello world;the counter is :94
The time server receive order :hello world;the counter is :95
The time server receive order :hello world;the counter is :96
The time server receive order :hello world;the counter is :97
The time server receive order :hello world;the counter is :98
The time server receive order :hello world;the counter is :99
The time server receive order :hello world;the counter is :100
The time server receive order :hello world;the counter is :101

 

客户端运行结果:

Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017
Now is :Sat Oct 21 23:17:57 CST 2017

续集

 

了解编解码基础知识之后,后面继续讲解Netty内置编解码框架的使用,例如Java序列化,二进制编解码,谷歌的protobuf和JBoss的Marshalling序列化框架。

 

0
0
分享到:
评论

相关推荐

    Netty 框架学习 —— 第一个 Netty 应用(csdn)————程序.pdf

    在本篇关于“Netty框架学习——第一个Netty应用”的文章中,我们将深入理解如何使用Netty构建一个简单的Echo服务器和客户端。Netty是一个高性能、异步事件驱动的网络应用程序框架,广泛应用于Java领域的服务器开发。...

    Netty案例集锦(并发编程篇).pdf

    本资料《Netty案例集锦(并发编程篇)》着重介绍了Netty在并发编程方面的应用,以下将详细阐述其中可能包含的知识点。 1. **Java NIO基础**:Netty基于Java NIO(非阻塞I/O)构建,NIO允许程序选择非阻塞方式读写数据...

    Netty权威指南 PDF电子书下载 带目录书签 完整版

    基础篇 走进Java NIO 入门篇 Netty NIO开发指南 中级篇 Netty编解码开发指南 高级篇 Netty多协议开发和应用 源码分析篇 Netty功能介绍和源码分析 架构和行业应用篇 Netty高级特性

    Netty最容易懂的实例和教材

    - **基础篇**:介绍Netty的基本架构,如ByteBuf(缓冲区)、ChannelFuture和Promise(异步操作)、ChannelHandlerContext(上下文对象)等概念,以及如何创建服务器和客户端。 - **进阶篇**:深入讲解Netty的...

    learning-netty.zip

    - **基础篇**: 介绍Netty的基本概念和架构,包括Channel、EventLoop、Pipeline等。 - **进阶篇**: 针对ByteBuf的使用、编码与解码、心跳机制等进行深入解析。 - **源码分析篇**: 分析Netty关键组件的内部实现,如...

    netty权威指南第2版

    基础篇走进Java NIO 第1章 Java的I/O演进之路 第2章 NIO入门 第3章 Netty入门应用 第4章 TCP粘包/拆包问题解析之道 第5章 分隔符和定长解析码器的应用 第6章 编码技术 第7章 MessagePack 编解码 第8章 Google ...

    Netty使用初步

    通过选择合适的模型,我们可以最大化地优化Netty应用的性能。 总结起来,Netty是一个强大的网络编程框架,它的异步事件驱动模型、高效的缓冲区机制、丰富的协议处理组件以及灵活的配置选项,都使得开发高性能、可...

    Netty学习教程之基础使用篇

    Netty是由JBOSS提供的一个Java开源框架。Netty提供异步的、事件驱动的网络应用程序框架和工具,用以快速开发高性能、高可靠性的...下面这篇文章主要给大家介绍了关于Netty基础使用的相关资料,需要的朋友可以参考下。

    使用jboss netty 创建高性能webservice客户端及服务端

    本篇文章将深入探讨如何利用JBoss Netty创建高效的Web Service客户端和服务端。 首先,我们需要理解Netty的基本工作原理。Netty采用非阻塞I/O模型,基于Java NIO(非阻塞输入/输出)库,允许在网络操作中进行高并发...

    基于Netty实现的文件上传

    本篇将深入探讨如何利用Netty实现文件上传功能,并结合Socket通信和UDP协议进行相关知识的扩展。 首先,我们来关注"基于Netty实现的文件上传"这一主题。Netty提供了丰富的ChannelHandler(通道处理器)和ByteBuf...

    netty下载

    在博客链接中提到的 https://qqggcc.iteye.com/blog/2252859,虽然具体内容无法在此直接查看,但可以推测这可能是一篇关于如何使用 Netty 或者在实际项目中应用 Netty 的技术文章。博客可能会涵盖 Netty 的基础概念...

    精通并发与Netty 涉及到的代码

    在Netty应用中,可以集成NSQ客户端库,实现生产者发布消息和消费者接收消息的功能。NSQ提供了消息持久化和多消费者订阅的特性,确保了消息的可靠传递。 Lombok是一个Java库,能自动插入编辑器和构建工具,提供了...

    基于netty的websocket开发小结

    这篇基于Netty的WebSocket开发小结将探讨如何使用Netty实现WebSocket服务端和客户端的交互。 首先,我们要理解WebSocket的基本概念。WebSocket协议定义了一种在单个TCP连接上进行全双工通信的协议。它通过在握手...

    Netty权威指南

    基础篇:java的IO演进之路; BIO ;NIO;伪异步;NIO类库 ; 入门篇:Jetty简单应用入门;TCP粘包拆包;定位符和定长解码器; 中级篇:编解码技术和常用的序列化框架(protobuf /java/Marshalling) 高级篇:Http协议...

    基于Netty实现CometStreaming方式的聊天室

    本篇将深入探讨如何利用Netty实现CometStreaming方式的聊天室。 首先,我们要理解CometStreaming是什么。Comet是一种Web技术,它允许服务器向浏览器推送数据,而不是传统的HTTP请求-响应模式。在聊天室场景中,这种...

    Netty4.0学习笔记系列之四:混合使用coder和handler

    在本篇Netty4.0学习笔记中,我们将聚焦于如何在实际应用中混合使用`coder`和`handler`,这是Netty框架中非常关键的一部分,对于构建高性能、低延迟的网络应用程序至关重要。Netty是一个用Java编写的异步事件驱动的...

    Netty4.0学习笔记系列之二:Handler的执行顺序

    总的来说,理解并熟练掌握 Netty 中 Handler 的执行顺序是开发高效、可扩展的网络应用的基础。通过合理地组织和调整 Handler 链,我们可以实现更高效的 I/O 处理,提高系统吞吐量,并降低资源消耗。在实际开发中,...

    netty websocket源码

    本篇将深入探讨Netty与WebSocket的结合,以及如何利用Netty构建WebSocket服务器。 首先,Netty是一个高性能、异步事件驱动的网络应用框架,它简化了Java网络编程,特别适合用于创建高并发、低延迟的服务。WebSocket...

    Netty4.0学习笔记系列之五:自定义通讯协议

    在本篇“Netty4.0学习笔记系列之五:自定义通讯协议”中,我们将深入探讨如何在Netty框架下构建和实现自己的通信协议。Netty是一个高性能、异步事件驱动的网络应用框架,广泛应用于Java领域的服务器开发,如网络游戏...

Global site tag (gtag.js) - Google Analytics