use of io.netty.handler.codec.string.StringDecoder 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());
}
use of 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());
// 客户端的handler
// 先调用handler在ChannnelActive方法中调用fireChannelActive会激活handler1
pipeline.addLast("handler", new HwClientHandler());
pipeline.addLast("handler1", new BaseClientHandler());
}
use of 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());
// 字符长度
pipeline.addLast(new LineBasedFrameDecoder(2048));
// 自己的逻辑Handler
pipeline.addLast("handler", new HelloWordServerHandler());
}
use of io.netty.handler.codec.string.StringDecoder in project tutorials-java by Artister.
the class NettyServer method service.
public static void service() throws Exception {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup, workerGroup);
bootstrap.channel(NioServerSocketChannel.class);
bootstrap.childHandler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
// TODO Auto-generated method stub
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
pipeline.addLast(new LengthFieldPrepender(4));
pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));
pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8));
pipeline.addLast(new TcpServerHandler());
}
});
ChannelFuture f = bootstrap.bind(IP, PORT).sync();
f.channel().closeFuture().sync();
System.out.println("TCP服务器已启动");
}
use of io.netty.handler.codec.string.StringDecoder in project flink by apache.
the class NettyClientServerSslTest method testSslPinningForInvalidFingerprint.
@Test
public void testSslPinningForInvalidFingerprint() throws Exception {
NettyProtocol protocol = new NoOpProtocol();
Configuration config = createSslConfig();
// pin the certificate based on internal cert
config.setString(SecurityOptions.SSL_INTERNAL_CERT_FINGERPRINT, SSLUtilsTest.getCertificateFingerprint(config, "flink.test").replaceAll("[0-9A-Z]", "0"));
NettyTestUtil.NettyServerAndClient serverAndClient;
try (NetUtils.Port port = NetUtils.getAvailablePort()) {
NettyConfig nettyConfig = createNettyConfig(config, port);
serverAndClient = NettyTestUtil.initServerAndClient(protocol, nettyConfig);
}
Assert.assertNotNull("serverAndClient is null due to fail to get a free port", serverAndClient);
Channel ch = NettyTestUtil.connect(serverAndClient);
ch.pipeline().addLast(new StringDecoder()).addLast(new StringEncoder());
assertFalse(ch.writeAndFlush("test").await().isSuccess());
NettyTestUtil.shutdown(serverAndClient);
}
Aggregations