use of org.apache.flink.shaded.netty4.io.netty.handler.codec.string.StringDecoder 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();
}
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.string.StringDecoder 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());
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.string.StringDecoder 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());
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.string.StringDecoder 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());
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.string.StringDecoder 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 HelloWorldServerHandler());
}
Aggregations