Netty UDP server 代码 服务器解析【自定义通讯协议】netty是3.5.3

最近在做RFID定位的项目,为了方便自主开发2.4G读写器和数据服务器。服务端采用Netty。


协议描述:

1. 标签ID数据上传:读写器设备按设定时间频率更新读到的所有电子标签的ID号,并向上位机主动发送数据(用UDP模式) 

设备ID(2Byte = 0-65535) + 'A' Sn(1Byte = 0-255) + 标签数(1Byte = n) + 标签1(3Byte = 0x00,0x2A,0x3B)+ 标签2(3Byte = 0x01,0x2b,0x3c)+....+标签n(3Byte = 0x00,0x23,0x6B) + CheckSum(1Byte)


[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.netty;  
  2.   
  3.   
  4. import java.net.InetSocketAddress;  
  5.   
  6. import org.jboss.netty.bootstrap.ConnectionlessBootstrap;  
  7. import org.jboss.netty.channel.ChannelPipeline;  
  8. import org.jboss.netty.channel.ChannelPipelineFactory;  
  9. import org.jboss.netty.channel.Channels;  
  10. import org.jboss.netty.channel.socket.DatagramChannelFactory;  
  11. import org.jboss.netty.channel.socket.nio.NioDatagramChannelFactory;  
  12.   
  13. public class UdpServer {  
  14.     private ConnectionlessBootstrap udpBootstrap;  
  15.     public UdpServer(int port){  
  16.         DatagramChannelFactory channelFactory = new NioDatagramChannelFactory();  
  17.         udpBootstrap = new ConnectionlessBootstrap(channelFactory);  
  18.         udpBootstrap.setPipelineFactory(new ChannelPipelineFactory() {  
  19.             public ChannelPipeline getPipeline() throws Exception {  
  20.                 return Channels.pipeline(new UdpEventHandler());  
  21.             }  
  22.         });  
  23.         udpBootstrap.bind(new InetSocketAddress(port));  
  24.     }  
  25.     public static void main(String[] args) {  
  26.         new UdpServer(32500);  
  27.     }  
  28. }  



[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.netty;  
  2.   
  3. import java.nio.ByteBuffer;  
  4.   
  5. import org.jboss.netty.buffer.ChannelBuffer;  
  6. import org.jboss.netty.channel.ChannelHandlerContext;  
  7. import org.jboss.netty.channel.ExceptionEvent;  
  8. import org.jboss.netty.channel.MessageEvent;  
  9. import org.jboss.netty.channel.SimpleChannelUpstreamHandler;  
  10.   
  11. public class UdpEventHandler extends SimpleChannelUpstreamHandler {  
  12.   
  13.     @Override  
  14.     public void exceptionCaught(ChannelHandlerContext arg0, ExceptionEvent arg1)  
  15.             throws Exception {  
  16.         super.exceptionCaught(arg0, arg1);  
  17.           
  18.     }  
  19.   
  20.     private ChannelBuffer buffer;  
  21.     private ByteBuffer byteBuffer;  
  22.     private byte[] arrayByte;  
  23.     private int deviceID ;      //设备ID  
  24.     private int cmd;            //命令类型  
  25.     private int sn;               
  26.     private int count ;         //标签数  
  27.     private int tag;            //标签号  
  28.     private int checkSum ;      //校验和  
  29.     private int tagType;        //标签类型  
  30.       
  31.       
  32.     @Override  
  33.     public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)  
  34.             throws Exception {  
  35.         super.messageReceived(ctx, e);  
  36.         buffer = (ChannelBuffer)e.getMessage();  
  37.         byteBuffer = buffer.copy().toByteBuffer();  
  38.         arrayByte = byteBuffer.array();  
  39.         if(  !(arrayByte!= null && arrayByte.length >=14) ){  
  40.             return ;  
  41.         }  
  42.         deviceID = toInt(arrayByte[6]);  
  43.         deviceID = (deviceID<<8) + toInt(arrayByte[7]);  
  44.         cmd =  toInt(arrayByte[8]);  
  45.         sn = toInt(arrayByte[9]);  
  46.         count = toInt(arrayByte[10]);  
  47.         if((char)cmd == 'A' ){  
  48.             if(count == 0 )  
  49.                 return  ;  
  50.             checkSum = toInt(arrayByte[11+(count*3)]);  
  51.               
  52.             //上报标签  
  53.             for(int i=0;i<count;i++){  
  54.                 tagType = toInt(arrayByte[11+(i*3)]);  
  55.                 tag = toInt(arrayByte[11+(i*3)+1]);  
  56.                 tag = (tag<<8) +toInt(arrayByte[11+(i*3)+2]);  
  57.                 //插入数据库  
  58.                   
  59.             }  
  60.               
  61.         }else if ((char)cmd == 'B'){  
  62.             //无标签上报心跳包       
  63.         }  
  64.     }  
  65.       
  66.     //取正     
  67.     private   int   toInt(byte   b){     
  68.             if(b   >=   0)   return   (int)b;     
  69.             else               return   (int)(b   +   256);     
  70.     }     
  71.   
  72.       
  73.       
  74.       
  75.       
  76.       
  77.       
  78.       
  79.       
  80.       
  81.       
  82. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值