package com.jsowell.netty.client; import com.jsowell.common.constant.Constants; import com.jsowell.common.util.DateUtils; import io.netty.bootstrap.Bootstrap; import io.netty.channel.ChannelFuture; 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.codec.string.StringDecoder; import io.netty.handler.codec.string.StringEncoder; import lombok.Data; import lombok.extern.slf4j.Slf4j; @Slf4j @Data public class NettyClient implements Runnable { static final String HOST = System.getProperty("host", Constants.SOCKET_IP); static final int PORT = Integer.parseInt(System.getProperty("port", "9011")); static final int SIZE = Integer.parseInt(System.getProperty("size", "256")); private String content; public NettyClient(String content) { this.content = content; } @Override public void run() { // Configure the client. EventLoopGroup group = new NioEventLoopGroup(); try { int num = 0; boolean boo = true; Bootstrap b = new Bootstrap(); b.group(group) .channel(NioSocketChannel.class) .option(ChannelOption.TCP_NODELAY, true) .handler(new NettyClientChannelInitializer() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast("decoder", new StringDecoder()); p.addLast("encoder", new StringEncoder()); p.addLast(new NettyClientHandler()); } }); ChannelFuture future = b.connect(HOST, PORT).sync(); while (boo) { num++; future.channel().writeAndFlush("发送数据=======" + content + "--" + DateUtils.getTime()); try { //休眠一段时间 Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } //每一条线程向服务端发送的次数 if (num == 5) { boo = false; } } log.info(content + "-----------------------------" + num); //future.channel().closeFuture().sync(); } catch (InterruptedException e) { e.printStackTrace(); } finally { group.shutdownGracefully(); } } /** * 下面是不加线程的 */ /*public static void main(String[] args) throws Exception { sendMessage("hhh你好?"); } public static void sendMessage(String content) throws InterruptedException { // Configure the client. EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(group) .channel(NioSocketChannel.class) .option(ChannelOption.TCP_NODELAY, true) .handler(new NettyClientChannelInitializer() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast("decoder", new StringDecoder()); p.addLast("encoder", new StringEncoder()); p.addLast(new NettyClientHandler()); } }); ChannelFuture future = b.connect(HOST, PORT).sync(); future.channel().writeAndFlush(content); future.channel().closeFuture().sync(); } finally { group.shutdownGracefully(); } }*/ }