Search in sources :

Example 31 with StringEncoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.string.StringEncoder in project pancm_project by xuwujing.

the class NettyServerDemo5 method 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 32 with StringEncoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.string.StringEncoder in project tutorials-java by Artister.

the class NettyClient method run.

public void run() {
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group);
        b.channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true);
        b.handler(new ChannelInitializer<SocketChannel>() {

            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline pipeline = ch.pipeline();
                pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
                pipeline.addLast("frameEncoder", new LengthFieldPrepender(4));
                pipeline.addLast("decoder", new StringDecoder(CharsetUtil.UTF_8));
                pipeline.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));
                pipeline.addLast("handler", new HelloClient());
            }
        });
        for (int i = 0; i < 10; i++) {
            ChannelFuture f = b.connect("127.0.0.1", 5656).sync();
            f.channel().writeAndFlush("hello Service!" + Thread.currentThread().getName() + ":--->:" + i);
            f.channel().closeFuture().sync();
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        group.shutdownGracefully();
    }
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) HelloClient(org.ko.netty.tn.handler.HelloClient) StringDecoder(io.netty.handler.codec.string.StringDecoder) LengthFieldPrepender(io.netty.handler.codec.LengthFieldPrepender) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) StringEncoder(io.netty.handler.codec.string.StringEncoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Bootstrap(io.netty.bootstrap.Bootstrap) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 33 with StringEncoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.string.StringEncoder in project tutorials-java by Artister.

the class ClientChannelInitializer method initChannel.

protected void initChannel(SocketChannel socketChannel) throws Exception {
    /**
     * Channel---通道
     * ChannelPipeline是ChannelHandler的容器
     * 负责ChannelHandler的管理和事件拦截与调度
     * 类似于Servlet 和Filter 过滤器
     * 方便事件的拦截和用户业务逻辑的定制
     */
    ChannelPipeline pipeline = socketChannel.pipeline();
    /**
     * 这个地方的 必须和服务端对应上。否则无法正常解码和编码
     * 解码和编码 我将会在下一节为大家详细的讲解。暂时不做详细的描述
     */
    pipeline.addLast("decoder", new StringDecoder());
    pipeline.addLast("encoder", new StringEncoder());
    // 我们自己的handler
    pipeline.addLast("handler", new HelloWorldClientHandler());
}
Also used : StringEncoder(io.netty.handler.codec.string.StringEncoder) HelloWorldClientHandler(org.ko.netty.t1.handler.HelloWorldClientHandler) StringDecoder(io.netty.handler.codec.string.StringDecoder) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 34 with StringEncoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.string.StringEncoder in project tutorials-java by Artister.

the class ClientChannelInitializer method initChannel.

protected void initChannel(SocketChannel socketChannel) throws Exception {
    ChannelPipeline pipeline = socketChannel.pipeline();
    pipeline.addLast("decoder", new StringDecoder());
    pipeline.addLast("encoder", new StringEncoder());
    // 客户端的逻辑
    pipeline.addLast("handler", new HelloWorldClientHandler());
}
Also used : StringEncoder(io.netty.handler.codec.string.StringEncoder) HelloWorldClientHandler(org.ko.netty.t3.handler.HelloWorldClientHandler) StringDecoder(io.netty.handler.codec.string.StringDecoder) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 35 with StringEncoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.string.StringEncoder in project tutorials-java by Artister.

the class ServerChannelInitializer method initChannel.

@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
    ChannelPipeline pipeline = socketChannel.pipeline();
    // 字符串解码 和 编码
    pipeline.addLast("decoder", new StringDecoder());
    pipeline.addLast("encoder", new StringEncoder());
    // 自己的逻辑Handler
    pipeline.addLast("handler", new HwServerHandler());
}
Also used : StringEncoder(io.netty.handler.codec.string.StringEncoder) HwServerHandler(org.ko.netty.t2.handler.HwServerHandler) StringDecoder(io.netty.handler.codec.string.StringDecoder) ChannelPipeline(io.netty.channel.ChannelPipeline)

Aggregations

StringEncoder (io.netty.handler.codec.string.StringEncoder)40 StringDecoder (io.netty.handler.codec.string.StringDecoder)37 ChannelPipeline (io.netty.channel.ChannelPipeline)28 SocketChannel (io.netty.channel.socket.SocketChannel)13 ChannelFuture (io.netty.channel.ChannelFuture)12 EventLoopGroup (io.netty.channel.EventLoopGroup)9 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)9 LengthFieldBasedFrameDecoder (io.netty.handler.codec.LengthFieldBasedFrameDecoder)9 LengthFieldPrepender (io.netty.handler.codec.LengthFieldPrepender)9 Bootstrap (io.netty.bootstrap.Bootstrap)8 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)8 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)7 Channel (io.netty.channel.Channel)7 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)7 DelimiterBasedFrameDecoder (io.netty.handler.codec.DelimiterBasedFrameDecoder)7 LineBasedFrameDecoder (io.netty.handler.codec.LineBasedFrameDecoder)7 NettyServerAndClient (org.apache.flink.runtime.io.network.netty.NettyTestUtil.NettyServerAndClient)5 Channel (org.apache.flink.shaded.netty4.io.netty.channel.Channel)5 SocketChannel (org.apache.flink.shaded.netty4.io.netty.channel.socket.SocketChannel)5 StringDecoder (org.apache.flink.shaded.netty4.io.netty.handler.codec.string.StringDecoder)5