Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.bounceme.dur.netty;
- import io.netty.bootstrap.Bootstrap;
- import io.netty.channel.ChannelFuture;
- import io.netty.channel.ChannelInitializer;
- import io.netty.channel.ChannelOption;
- import io.netty.channel.ChannelPipeline;
- import io.netty.channel.EventLoopGroup;
- import io.netty.channel.nio.NioEventLoopGroup;
- import io.netty.channel.socket.SocketChannel;
- import io.netty.channel.socket.nio.NioSocketChannel;
- import io.netty.handler.logging.LogLevel;
- import io.netty.handler.logging.LoggingHandler;
- import io.netty.handler.ssl.SslContext;
- import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
- public final class EchoClient {
- private boolean ssl;
- private String host;
- private int port;
- static final int SIZE = Integer.parseInt(System.getProperty("size", "256"));
- private final MyProps p = new MyProps();
- public static void main(String[] args) throws Exception {
- new EchoClient().runServer();
- }
- private void runServer() throws Exception {
- ssl = p.getSSL();
- host = p.getHost();
- port = p.getServerPort();
- final SslContext sslCtx;
- if (ssl) {
- sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE);
- } else {
- sslCtx = null;
- }
- EventLoopGroup group = new NioEventLoopGroup();
- try {
- Bootstrap b = new Bootstrap();
- b.group(group)
- .channel(NioSocketChannel.class)
- .option(ChannelOption.TCP_NODELAY, true)
- .handler(new ChannelInitializer<SocketChannel>() {
- @Override
- public void initChannel(SocketChannel ch) throws Exception {
- ChannelPipeline p = ch.pipeline();
- if (sslCtx != null) {
- p.addLast(sslCtx.newHandler(ch.alloc(), host, port));
- }
- p.addLast(new LoggingHandler(LogLevel.INFO));
- p.addLast(new PingPongClientHandler());
- }
- });
- ChannelFuture f = b.connect(host, port).sync();
- f.channel().closeFuture().sync();
- } finally {
- group.shutdownGracefully();
- }
- }
- static int getSize() {
- return SIZE;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment