package com.xcong.excoin.netty.client; import io.netty.bootstrap.Bootstrap; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; 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.codec.DelimiterBasedFrameDecoder; import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringEncoder; import java.net.InetSocketAddress; /** * @author wzy * @email wangdoubleone@gmail.com * @date 2019-05-14 */ public class ClientServer { public static void start() throws InterruptedException { EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(group) .channel(NioSocketChannel.class) .remoteAddress(new InetSocketAddress("localhost", 9998)) .handler(new ChannelInitializer() { //5 @Override public void initChannel(SocketChannel ch) throws Exception { ByteBuf buf = Unpooled.copiedBuffer("_split".getBytes()); ch.pipeline().addLast(new DelimiterBasedFrameDecoder(10240, buf)); ch.pipeline().addLast(new StringDecoder()); ch.pipeline().addLast(new StringEncoder()); ch.pipeline().addLast(new ClientChannelHandler()); } }); ChannelFuture f = b.connect().sync(); f.channel().closeFuture().sync(); } finally { group.shutdownGracefully(); } } public static void main(String args[]) throws InterruptedException { start(); } }