package com.casic.missiles.client; import com.casic.missiles.modular.system.util.RSAUtil; import io.netty.bootstrap.Bootstrap; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelOption; 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.StringEncoder; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.nio.charset.StandardCharsets; /** * @description: 模拟发送环境数据,测试用 * @author: Stone * @create: 2019-01-13 21:58 **/ @Slf4j @Component public class HjtClient { public static void main(String[] args) { // for (int i = 0; i < 1; i++) // connect("127.0.0.1",9091,i+"-->bootstrap.group(workGroup).channel(NioSocketChannel.class).option(ChannelOption.SO_KEEPALIVE, true)" + // "bootstrap.group(workGroup).channel(NioSocketChannel.class).option(ChannelOption.SO_KEEPALIVE, true)" + // "bootstrap.group(workGroup).channel(NioSocketChannel.class).option(ChannelOption.SO_KEEPALIVE, true)" + // "bootstrap.group(workGroup).channel(NioSocketChannel.class).option(ChannelOption.SO_KEEPALIVE, true)" + // "bootstrap.group(workGroup).channel(NioSocketChannel.class).option(ChannelOption.SO_KEEPALIVE, true)" + // "bootstrap.group(workGroup).channel(NioSocketChannel.class).option(ChannelOption.SO_KEEPALIVE, true)" + // "bootstrap.group(workGroup).channel(NioSocketChannel.class).option(ChannelOption.SO_KEEPALIVE, true)" + // "bootstrap.group(workGroup).channel(NioSocketChannel.class).option(ChannelOption.SO_KEEPALIVE, true)" + // "bootstrap.group(workGroup).channel(NioSocketChannel.class).option(ChannelOption.SO_KEEPALIVE, true)"); connect("127.0.0.1", 19110, "257"); } public static void connect(String host, int port, String msg) { EventLoopGroup workGroup = new NioEventLoopGroup(); Bootstrap bootstrap = new Bootstrap(); bootstrap.group(workGroup).channel(NioSocketChannel.class).option(ChannelOption.SO_KEEPALIVE, true) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel socketChannel) throws Exception { socketChannel.pipeline().addLast(new StringEncoder()); } }); ChannelFuture cf = null; try { cf = bootstrap.connect(host, port).sync(); // cf.channel().writeAndFlush(Unpooled.copiedBuffer(ByteUtil.hexStringToBytes(msg))); // cf.channel().writeAndFlush(msg); // cf.channel().writeAndFlush(Unpooled.copiedBuffer(("^"+msg+";2322AA;").getBytes(StandardCharsets.UTF_8))); String msgStr = MsgUtil.getMsg(msg, "1", ""); String encodeMsgStr = RSAUtil.getMsg(msgStr) ; // cf.channel().writeAndFlush(encodeMsgStr.getBytes(StandardCharsets.UTF_8)); cf.channel().writeAndFlush(Unpooled.copiedBuffer(encodeMsgStr.getBytes(StandardCharsets.UTF_8))); if (cf.isSuccess()) { log.info("[{}] : channelId = {}", cf.channel().remoteAddress(), "已发送采集数据->" + msg); } } catch (InterruptedException e) { e.printStackTrace(); } finally { // workGroup.shutdownGracefully(); } } }