package com.kwan.shuyu.onlytest; import io.netty.bootstrap.Bootstrap; import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufAllocator; import io.netty.channel.Channel; import io.netty.channel.ChannelInitializer; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringEncoder; import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; import lombok.extern.slf4j.Slf4j; @Slf4j public class Client { public static void main(String[] args) throws InterruptedException { final NioEventLoopGroup group = new NioEventLoopGroup(); LoggingHandler LOGGING_HANDLER = new LoggingHandler(LogLevel.DEBUG); try { final Channel channel = new Bootstrap() .group(group) .channel(NioSocketChannel.class) .handler(new ChannelInitializer() { @Override protected void initChannel(NioSocketChannel ch) throws Exception { ch.pipeline().addLast(LOGGING_HANDLER); ch.pipeline().addLast(new StringEncoder()); ch.pipeline().addLast(new StringDecoder()); } }) .connect("localhost", 7777).sync().channel(); final ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(); buf.writeBytes("abcdfmw9".getBytes()); channel.writeAndFlush( buf ).addListener(promise -> { if (!promise.isSuccess()) { Throwable cause = promise.cause(); log.error("error : ", cause); } }); channel.closeFuture().sync(); } catch (InterruptedException e) { log.error("client error", e); } finally { group.shutdownGracefully(); } } }