Search in sources :

Example 51 with StringDecoder

use of io.netty.handler.codec.string.StringDecoder in project pancm_project by xuwujing.

the class NettyClient method main.

/**
 * Netty创建全部都是实现自AbstractBootstrap。
 * 客户端的是Bootstrap,服务端的则是	ServerBootstrap。
 *
 * @param args the input arguments
 * @throws Exception the exception
 */
public static void main(String[] args) throws Exception {
    try {
        b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true).handler(new ChannelInitializer<SocketChannel>() {

            @Override
            public void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline p = ch.pipeline();
                p.addLast("decoder", new StringDecoder());
                p.addLast("encoder", new StringEncoder());
                p.addLast(new BaseClient1Handler());
                p.addLast(new BaseClient2Handler());
            }
        });
        // 连接服务端
        ChannelFuture future = b.connect(host, port).sync();
        System.out.println("客户端连接成功!");
        // 发送消息
        future.channel().writeAndFlush("Hello Netty Server ,I am a common client");
        // 关闭
        future.channel().closeFuture().sync();
    } finally {
        // 释放资源
        group.shutdownGracefully();
    }
// start();
}
Also used : StringEncoder(io.netty.handler.codec.string.StringEncoder) ChannelFuture(io.netty.channel.ChannelFuture) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) StringDecoder(io.netty.handler.codec.string.StringDecoder) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 52 with StringDecoder

use of io.netty.handler.codec.string.StringDecoder in project pancm_project by xuwujing.

the class NettyServerDemo5 method start.

/**
 * Start.
 */
public void start() {
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap sbs = new ServerBootstrap().group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).localAddress(new InetSocketAddress(port)).childHandler(new ChannelInitializer<SocketChannel>() {

            protected void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline p = ch.pipeline();
                // 入参说明: 读超时时间、写超时时间、所有类型的超时时间、时间格式
                p.addLast(new IdleStateHandler(5, 0, 0, TimeUnit.SECONDS));
                // String解码器
                p.addLast(new StringDecoder());
                // String编码器
                p.addLast(new StringEncoder());
                // 绑定自定义业务逻辑
                p.addLast(new NettyServerHandlerDemo5());
            }
        }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true);
        // 绑定端口,开始接收进来的连接
        ChannelFuture future = sbs.bind(port).sync();
        System.out.println("Netty服务端启动成功,端口为: " + port);
        // 释放监听
        future.channel().closeFuture().sync();
    } catch (Exception e) {
        // 释放资源
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) InetSocketAddress(java.net.InetSocketAddress) StringDecoder(io.netty.handler.codec.string.StringDecoder) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelPipeline(io.netty.channel.ChannelPipeline) StringEncoder(io.netty.handler.codec.string.StringEncoder) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 53 with StringDecoder

use of io.netty.handler.codec.string.StringDecoder in project pancm_project by xuwujing.

the class NettyServer method start.

/**
 * Netty创建全部都是实现自AbstractBootstrap。
 * 客户端的是Bootstrap,服务端的则是	ServerBootstrap。
 *
 * @throws InterruptedException the interrupted exception
 */
public static void start() throws InterruptedException {
    // 引导辅助程序
    ServerBootstrap sb = new ServerBootstrap();
    // 通过nio方式来接收连接和处理连接
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        // 通过nio方式来接收连接和处理连接
        sb.group(group);
        // 设置nio类型的channel
        sb.channel(NioServerSocketChannel.class);
        sb.childHandler(new // 有连接到达时会创建一个channel
        ChannelInitializer<SocketChannel>() {

            protected void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline p = ch.pipeline();
                // 字符串解码 和 编码
                p.addLast("decoder", new StringDecoder());
                p.addLast("encoder", new StringEncoder());
                // 在channel队列中添加一个handler来处理业务
                p.addLast("handler", new NettyServerHandler());
            // 以("\n")为结尾分割的 解码器
            // cp.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
            }
        });
        // 配置完成,开始绑定server,通过调用sync同步方法阻塞直到绑定成功
        ChannelFuture f = sb.bind(port).sync();
        System.out.println("服务端已启动... 端口是:" + port);
        // 应用程序会一直等待,直到channel关闭
        f.channel().closeFuture().sync();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
    // group.shutdownGracefully().sync();//关闭EventLoopGroup,释放掉所有资源包括创建的线程
    }
}
Also used : StringEncoder(io.netty.handler.codec.string.StringEncoder) ChannelFuture(io.netty.channel.ChannelFuture) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) StringDecoder(io.netty.handler.codec.string.StringDecoder) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 54 with StringDecoder

use of io.netty.handler.codec.string.StringDecoder in project pancm_project by xuwujing.

the class NettyServerDemo2 method start.

/**
 * Start.
 */
public void start() {
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap sbs = new ServerBootstrap().group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).localAddress(new InetSocketAddress(port)).childHandler(new ChannelInitializer<SocketChannel>() {

            protected void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline p = ch.pipeline();
                // 字节解码器 ,其中2048是规定一行数据最大的字节数。  用于解决拆包问题
                p.addLast(new LineBasedFrameDecoder(2048));
                // p.addLast(new FixedLengthFrameDecoder(100));   //定长数据帧的解码器 ,每帧数据100个字节就切分一次。  用于解决粘包问题
                // p.addLast(new DelimiterBasedFrameDecoder(1024,Unpooled.copiedBuffer("~_~".getBytes()))); //固定字符切分解码器 ,会以"~_~"为分隔符。  注意此方法要放到StringDecoder()上面
                // 设置解码器
                p.addLast(new StringDecoder());
                // 绑定自定义事物
                p.addLast(new NettyServerHandlerDemo2());
            }
        }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true);
        // 绑定端口,开始接收进来的连接
        ChannelFuture future = sbs.bind(port).sync();
        System.out.println("服务端启动成功,端口为 :" + port);
        future.channel().closeFuture().sync();
    } catch (Exception e) {
        // 关闭EventLoopGroup,释放掉所有资源包括创建的线程
        bossGroup.shutdownGracefully();
        // 关闭EventLoopGroup,释放掉所有资源包括创建的线程
        workerGroup.shutdownGracefully();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) InetSocketAddress(java.net.InetSocketAddress) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) StringDecoder(io.netty.handler.codec.string.StringDecoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 55 with StringDecoder

use of io.netty.handler.codec.string.StringDecoder in project sidewinder by srotya.

the class GraphiteServer method start.

@Override
public void start() throws Exception {
    bossGroup = new NioEventLoopGroup(1);
    workerGroup = new NioEventLoopGroup(2);
    ServerBootstrap bs = new ServerBootstrap();
    channel = bs.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_RCVBUF, 10485760).handler(new LoggingHandler(LogLevel.DEBUG)).childHandler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline p = ch.pipeline();
            p.addLast(workerGroup, new LineBasedFrameDecoder(1024, true, true));
            p.addLast(workerGroup, new StringDecoder());
            p.addLast(workerGroup, new GraphiteDecoder(dbName, storageEngine, writeCounter));
        }
    }).bind(bindAddress, serverPort).sync().channel();
}
Also used : NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) LoggingHandler(io.netty.handler.logging.LoggingHandler) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) StringDecoder(io.netty.handler.codec.string.StringDecoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelPipeline(io.netty.channel.ChannelPipeline)

Aggregations

StringDecoder (io.netty.handler.codec.string.StringDecoder)63 StringEncoder (io.netty.handler.codec.string.StringEncoder)42 ChannelPipeline (io.netty.channel.ChannelPipeline)35 SocketChannel (io.netty.channel.socket.SocketChannel)27 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)21 ChannelFuture (io.netty.channel.ChannelFuture)18 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)16 DelimiterBasedFrameDecoder (io.netty.handler.codec.DelimiterBasedFrameDecoder)16 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)15 Bootstrap (io.netty.bootstrap.Bootstrap)14 EventLoopGroup (io.netty.channel.EventLoopGroup)14 LineBasedFrameDecoder (io.netty.handler.codec.LineBasedFrameDecoder)14 LengthFieldPrepender (io.netty.handler.codec.LengthFieldPrepender)13 LengthFieldBasedFrameDecoder (io.netty.handler.codec.LengthFieldBasedFrameDecoder)12 Channel (io.netty.channel.Channel)11 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)11 LoggingHandler (io.netty.handler.logging.LoggingHandler)8 ChannelHandler (io.netty.channel.ChannelHandler)6 IdleStateHandler (io.netty.handler.timeout.IdleStateHandler)6 Test (org.junit.Test)6