netty:是一个NIO框架,对socket的封装,并处理了jdk网络编程中的一些异常,功能强大。分为服务端和客户端。
应用场景:网络层数据通信
接收网关发送的数据帧,并回应数据帧,通信协议采用TCP/IP。图片为数据帧格式:
public class NettyServer {
private final int port;
public NettyServer(int port) {
this.port = port;
}
public void start() throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup group = new NioEventLoopGroup();
try {
ServerBootstrap sb = new ServerBootstrap();
//允许指定一个@link channeloption,用于创建@link channel实例后的@link channel。使用@code NULL值删除前一组@link channeloption。
sb.option(ChannelOption.SO_BACKLOG, 1024);
sb.group(group, bossGroup) // 绑定线程池
.channel(NioServerSocketChannel.class) // 指定使用的channel
.localAddress(this.port)
.option(ChannelOption.TCP_NODELAY, true)
.option(ChannelOption.SO_KEEPALIVE, true)
.childHandler(new ServerChannelInitializer());
ChannelFuture cf = sb.bind().sync(); // 服务器异步创建绑定
System.out.println(NettyServer.class + " 启动正在监听: " + cf.channel().localAddress());
cf.channel().closeFuture().sync(); // 关闭服务器通道
} finally {
group.shutdownGracefully().sync(); // 释放线程池资源
bossGroup.shutdownGracefully().sync();
}
}
public static void main(String[] args) throws Exception{
new NettyServer(8888).start();
}
}
构建初始化器:
public class ServerChannelInitializer extends ChannelInitializer{
@Override
protected void initChannel(SocketChannel ch) throws Exception {
//日志
ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO));
//监测心跳
ch.pipeline().addLast("ping", new IdleStateHandler(60, 20, 60 * 10, TimeUnit.SECONDS));
//解码器
ch.pipeline().addLast(new Decoder());
ch.pipeline().addLast(new NettyServerHandler());
}
}
Decoder(解码器)功能:
public class Decoder extends ByteToMessageDecoder{
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List
NettyServerHandler(处理器)方法介绍:
public class NettyServerHandler extends SimpleChannelInboundHandler {
//应答数据帧
private List frames = new ArrayList();
/**
* 向网关发送数据
*/
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("网关与服务端通道-开启:" + ctx.channel().localAddress() + "channelActive");
for(byte[] frame : frames) {
ctx.writeAndFlush(Unpooled.copiedBuffer(frame));
System.out.println(ctx.channel().localAddress()+":\n发送数据:"+HexUtil.bytesToHexString(frame));
System.out.println("------------------------------");
}
}
/**
* channelInactive
*
* channel 通道 Inactive 不活跃的
*
* 当客户端主动断开服务端的链接后,这个通道就是不活跃的。也就是说客户端与服务端的关闭了通信通道并且不可以传输数据
*
*/
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
System.out.println("客户端与服务端通道-关闭:" + ctx.channel().localAddress() + "channelInactive");
}
@Override
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
System.out.println("读取通道信息..");
ByteBuf dataBuf = msg.readBytes(msg.readableBytes()-Message.BYTE_CRC-Message.BYTE_FRAME_END);
//获取crc码
String crc = ByteBufUtil.hexDump(msg.readBytes(Message.BYTE_CRC));
//校验数据
String crc16_ccitt = CRC16Util.CRC16_ccitt(ByteBufUtil.getBytes(dataBuf));
//数据
String dataStr = ByteBufUtil.hexDump(dataBuf);
System.out.println("------------------------------");
System.out.println(ctx.channel().localAddress()+"\n接收数据:"+dataStr);
//数据处理
if(crc.equalsIgnoreCase(crc16_ccitt)) {
//接收成功
frames = new Handler().handler(dataStr,Message.CHECK_SUCCESS);
}else {
//接收失败
frames = new Handler().handler(dataStr,Message.CHECK_FAILURE);
}
channelActive(ctx);//调用应答方法,应答数据帧
msg.retain();//尤为重要!!不然会报错。由于ByteBul有使用计数设置,目的是防止内存泄漏,
//此方法重置计数
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.close();
System.out.println("异常退出:" + cause.getMessage());
}
}
附上crc ccitt校验工具,上代码
http://www.ip33.com/crc.html 此链接为crc在线校验,生成多项式类型
public class CRC16Util {
private static int CRC16_ccitt_table[] = { 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, 0x8c48, 0x9dc1, 0xaf5a,
0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7, 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876, 0x2102, 0x308b, 0x0210, 0x1399, 0x6726,
0x76af, 0x4434, 0x55bd, 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5, 0x3183, 0x200a,
0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c, 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd,
0xc974, 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb, 0xce4c, 0xdfc5, 0xed5e, 0xfcd7,
0x8868, 0x99e1, 0xab7a, 0xbaf3, 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a, 0xdecd,
0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72, 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab,
0x0630, 0x17b9, 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1, 0x7387, 0x620e, 0x5095,
0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738, 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7, 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64,
0x5fed, 0x6d76, 0x7cff, 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036, 0x18c1, 0x0948,
0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e, 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c,
0xd1b5, 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd, 0xb58b, 0xa402, 0x9699, 0x8710,
0xf3af, 0xe226, 0xd0bd, 0xc134, 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c, 0xc60c,
0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3, 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9,
0x2f72, 0x3efb, 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232, 0x5ac5, 0x4b4c, 0x79d7,
0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a, 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9, 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab,
0xa022, 0x92b9, 0x8330, 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78 };
public static String CRC16_ccitt(byte [] pSrcData) {
int crc_reg = 0x0000;
for (int i = 0; i < pSrcData.length; i++) {
crc_reg = CRC16_ccitt_table[(crc_reg ^ pSrcData[i]) & 0xFF] ^ (crc_reg >> 8);
}
String hexString = Integer.toHexString(crc_reg).toUpperCase();
if(hexString.length()<4) {
hexString = "0"+hexString;
}
return hexString;
}
}
Message常量:
public class Message {
//反馈信息
public static final Boolean CHECK_SUCCESS = true;//接收成功
public static final Boolean CHECK_FAILURE = false;//接收失败
public static final String SUCCESS = "00";//接收成功
public static final String FAILURE = "01";//接收失败
public static final String FRAME_HEAD = "FEFE";//数据帧头
public static final String FRAME_END = "EEEE";//数据帧尾
public static final String REGISTER_DATA_LENGTH = "0003";//注册应答帧有效数据长度
public static final String HEART_DATA_LENGTH = "0001";//心跳应答帧有效数据长度
public static final String STATUS_DATA_LENGTH = "0001";//设备状态应答帧有效数据长度
public static final String IP_SWITCHING_DATA_LENGTH = "0006";//ip切换数据帧长度
public static final String ONLINE = "00";//设备离线
public static final String NOT_ONLINE = "01";//设备在线
//命令码
public static final String COMMAND_REGISTER = "10";//注册帧命令码
public static final String COMMAND_HEART_BEAT = "11";//心跳帧命令码
public static final String COMMAND_STATUS = "12";//终端状态帧命令码
public static final String COMMAND_EQUIPMENT_LIST = "80";//网关设备列表帧命令码
public static final String COMMAND_IP_SWITCHING = "8A";//ip切换帧命令码
//截取数据常量(所占字节数)
public static final Integer BYTE_BASE = 2 + 1 + 1 + 6 + 1 +2 + 2 + 2;//基本数据帧
public static final Integer BYTE_FRAME_HEAD = 2;
public static final Integer BYTE_VERSION = 1;//版本
public static final Integer BYTE_FRAME_NO = 1;//帧序号
public static final Integer BYTE_ADDRESS = 6;//地址
public static final Integer BYTE_COMMAND_CODE = 1;//命令码
public static final Integer BYTE_DATA_BODY_LENGTH = 2;//有效
public static final Integer BYTE_CRC = 2;//校验码
public static final Integer BYTE_FRAME_END = 2;//帧尾
public static final Integer BYTE_ONLINE = 1;//网关发送终端状态帧-是否在线
public static final Integer BYTE_MAC = 6;//mac地址
public static final Integer BYTE_MA = 2;//网关发送终端状态帧-电流
//截取字符串常量
public static final Integer START_INDEX = 0; //截取初始位置
public static final Integer LENGTH_STATUS_ONE_EQUIPMENT = 18;//网关发送终端状态帧---一个设备数据
public static final Integer LENGTH_STRING_HEX = 2;//2个字符对应一个16进制数据
//安全
public static final Integer BYTE_SOCKET = 2048;
//格式
public static final String HEAD_FLAG = "FEFE";
}
16进制数据交互工具:
public class HexUtil {
/**
* 16进制字符串转byte数组
* @param hexString
* @return
*/
public static byte[] hexStringToBytes(String hexString) {
if (hexString == null || hexString.equals("")) {
return null;
}
hexString = hexString.toUpperCase();
int length = hexString.length() / 2;
char[] hexChars = hexString.toCharArray();
byte[] d = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
}
return d;
}
private static byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
}
/**
* byte转为16进制字符串
* @param src
* @return
*/
public static String bytesToHexString(byte[] src){
StringBuilder stringBuilder = new StringBuilder("");
if (src == null || src.length <= 0) {
return null;
}
for (int i = 0; i < src.length; i++) {
int v = src[i] & 0xFF;
String hv = Integer.toHexString(v);
if (hv.length() < 2) {
stringBuilder.append(0);
}
stringBuilder.append(hv);
}
return stringBuilder.toString();
}
}
第一次使用netty,之前都是web开发,现在所在公司是做物联网大数据,人工智能这块,还是很有意思,踩了几天的坑,今天总算搞完了