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());
}
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());
}
});
}
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();
}
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;
}
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());
}
Aggregations