Search in sources :

Example 16 with StringEncoder

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

the class SecureChatClientInitializer method initChannel.

@Override
public void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();
    // Add SSL handler first to encrypt and decrypt everything.
    // In this example, we use a bogus certificate in the server side
    // and accept any invalid certificates in the client side.
    // You will need something more complicated to identify both
    // and server in the real world.
    pipeline.addLast(sslCtx.newHandler(ch.alloc(), SecureChatClient.HOST, SecureChatClient.PORT));
    // On top of the SSL handler, add the text line codec.
    pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
    pipeline.addLast(new StringDecoder());
    pipeline.addLast(new StringEncoder());
    // and then business logic.
    pipeline.addLast(new SecureChatClientHandler());
}
Also used : StringEncoder(io.netty.handler.codec.string.StringEncoder) DelimiterBasedFrameDecoder(io.netty.handler.codec.DelimiterBasedFrameDecoder) StringDecoder(io.netty.handler.codec.string.StringDecoder) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 17 with StringEncoder

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

the class ShutdownTask method run.

@Override
public void run(final String[] args) {
    ShutdownArgs shutdownArgs = parse(args);
    client.run("127.0.0.1", shutdownArgs.getShutdownPort().get(), new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(final SocketChannel ch) throws Exception {
            ChannelPipeline pipeline = ch.pipeline();
            pipeline.addLast("encoder", new StringEncoder());
            pipeline.addLast("handler", new ShutdownHandler());
        }
    });
}
Also used : StringEncoder(io.netty.handler.codec.string.StringEncoder) SocketChannel(io.netty.channel.socket.SocketChannel) ShutdownArgs(com.github.dreamhead.moco.bootstrap.ShutdownArgs) ConnectException(java.net.ConnectException) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 18 with StringEncoder

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

the class NettyClient method main.

public static void main(String[] args) throws InterruptedException {
    // 1、创建客户端启动类
    Bootstrap client = new Bootstrap();
    // 2、定义线程组,处理读写和链接事件,没有了accept事件
    EventLoopGroup group = new NioEventLoopGroup();
    client.group(group);
    // 3、绑定客户端通道
    client.channel(NioSocketChannel.class);
    // 4、给NIoSocketChannel初始化handler, 处理读写事件
    client.handler(new ChannelInitializer<NioSocketChannel>() {

        @Override
        protected void initChannel(NioSocketChannel ch) throws Exception {
            // 字符串编码器,一定要加在SimpleClientHandler 的上面
            ch.pipeline().addLast(new StringEncoder());
            // 基于分隔符的帧解码器
            ch.pipeline().addLast(new DelimiterBasedFrameDecoder(Integer.MAX_VALUE, Delimiters.lineDelimiter()[0]));
            // 通道入站处理器(用来处理服务端返回的数据)
            ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {

                @Override
                public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                    if (msg instanceof ByteBuf) {
                        String value = ((ByteBuf) msg).toString(Charset.defaultCharset());
                        System.out.println("Server=>" + value);
                    }
                    // 把客户端的通道关闭
                    ctx.channel().close();
                }
            });
        }
    });
    // 5、连接服务器
    ChannelFuture future = client.connect("localhost", 8080).sync();
    // 6、推送数据
    for (int i = 0; i < 5; i++) {
        future.channel().writeAndFlush("hello-" + i + "\r\n");
    }
    // 
    future.channel().closeFuture().sync();
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) DelimiterBasedFrameDecoder(io.netty.handler.codec.DelimiterBasedFrameDecoder) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ByteBuf(io.netty.buffer.ByteBuf) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) StringEncoder(io.netty.handler.codec.string.StringEncoder) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Bootstrap(io.netty.bootstrap.Bootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter)

Example 19 with StringEncoder

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

the class AttachmentTransferServer method startServer.

public boolean startServer(final char[] password) {
    boolean result = false;
    // If a password has been specified, create an EncryptionManager
    if (password != null && password.length > 0) {
        encryptionManager = new EncryptionManager(password);
    }
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(eventLoopGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_KEEPALIVE, true).childHandler(new ChannelInitializer<SocketChannel>() {

            @Override
            public void initChannel(final SocketChannel ch) {
                ch.pipeline().addLast(new DelimiterBasedFrameDecoder(((TRANSFER_BUFFER_SIZE + 2) / 3) * 4 + PATH_MAX, true, Delimiters.lineDelimiter()), new StringEncoder(CharsetUtil.UTF_8), new StringDecoder(CharsetUtil.UTF_8), new Base64Encoder(), new Base64Decoder(), new ServerTransferHandler());
            }
        });
        // Start the server.
        final ChannelFuture future = b.bind(port).sync();
        if (future.isDone() && future.isSuccess()) {
            result = true;
            logger.info("File Transfer Server started successfully");
        } else {
            logger.info("Failed to start the File Transfer Server");
        }
    } catch (final InterruptedException e) {
        logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
        stopServer();
    }
    return result;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) EncryptionManager(jgnash.util.EncryptionManager) DelimiterBasedFrameDecoder(io.netty.handler.codec.DelimiterBasedFrameDecoder) StringDecoder(io.netty.handler.codec.string.StringDecoder) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) Base64Encoder(io.netty.handler.codec.base64.Base64Encoder) StringEncoder(io.netty.handler.codec.string.StringEncoder) Base64Decoder(io.netty.handler.codec.base64.Base64Decoder)

Example 20 with StringEncoder

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

the class HelloClientInitializer method initChannel.

@Override
protected void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();
    /*
         * 这个地方的 必须和服务端对应上。否则无法正常解码和编码
         * 
         * 解码和编码 我将会在下一张为大家详细的讲解。再次暂时不做详细的描述
         * 
         * */
    pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
    pipeline.addLast("decoder", new StringDecoder());
    pipeline.addLast("encoder", new StringEncoder());
    // 客户端的逻辑
    pipeline.addLast("handler", new HelloClientHandler());
}
Also used : StringEncoder(io.netty.handler.codec.string.StringEncoder) DelimiterBasedFrameDecoder(io.netty.handler.codec.DelimiterBasedFrameDecoder) 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