public class UdpServerHandler extends SimpleChannelInboundHandler<DatagramPacket>{
private static Logger log = Logger.getLogger(UdpServerHandler.class);
@Override
protected void channelRead0(ChannelHandlerContext arg0, DatagramPacket arg1)
throws Exception {
// TODO Auto-generated method stub
ByteBuf buf = arg1.copy().content();
byte[] req = new byte[buf.readableBytes()];
buf.readBytes(req);
String s = bytes2HexString(req);
log.info("s : = " +s);
String responseStr ="686500650068100917684B00026000000400021000010001172012091124200416";
byte[] response_byte = hexString2Bytes(responseStr);
arg0.writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer(response_byte), arg1.sender()));
}
/**
* 16进制字节数组转换成16进制字符串
* @param b
* @return
*/
public static String bytes2HexString(byte[] b) {
StringBuffer result = new StringBuffer();
String hex;
for (int i = 0; i < b.length; i++) {
hex = Integer.toHexString(b[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
result.append(hex.toUpperCase());
}
return result.toString();
}
public static byte[] hexString2Bytes(String src) {
int l = src.length() / 2;
byte[] ret = new byte[l];
for (int i = 0; i < l; i++) {
ret[i] = (byte) Integer
.valueOf(src.substring(i * 2, i * 2 + 2), 16).byteValue();
}
return ret;
}
public static String makeChecksum(String data) {
if (data == null || data.equals("")) {
return "";
}
int total = 0;
int len = data.length();
int num = 0;
while (num < len) {
String s = data.substring(num, num + 2);
// System.out.println(s);
total += Integer.parseInt(s, 16);
num = num + 2;
}
/**
* 用256求余最大是255,即16进制的FF
*/
int mod = total % 256;
String hex = Integer.toHexString(mod);
len = hex.length();
// 如果不够校验位的长度,补0,这里用的是两位校验
if (len < 2) {
hex = "0" + hex;
}
return hex;
}
}
private static Logger log = Logger.getLogger(UdpServerHandler.class);
@Override
protected void channelRead0(ChannelHandlerContext arg0, DatagramPacket arg1)
throws Exception {
// TODO Auto-generated method stub
ByteBuf buf = arg1.copy().content();
byte[] req = new byte[buf.readableBytes()];
buf.readBytes(req);
String s = bytes2HexString(req);
log.info("s : = " +s);
String responseStr ="686500650068100917684B00026000000400021000010001172012091124200416";
byte[] response_byte = hexString2Bytes(responseStr);
arg0.writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer(response_byte), arg1.sender()));
}
/**
* 16进制字节数组转换成16进制字符串
* @param b
* @return
*/
public static String bytes2HexString(byte[] b) {
StringBuffer result = new StringBuffer();
String hex;
for (int i = 0; i < b.length; i++) {
hex = Integer.toHexString(b[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
result.append(hex.toUpperCase());
}
return result.toString();
}
public static byte[] hexString2Bytes(String src) {
int l = src.length() / 2;
byte[] ret = new byte[l];
for (int i = 0; i < l; i++) {
ret[i] = (byte) Integer
.valueOf(src.substring(i * 2, i * 2 + 2), 16).byteValue();
}
return ret;
}
public static String makeChecksum(String data) {
if (data == null || data.equals("")) {
return "";
}
int total = 0;
int len = data.length();
int num = 0;
while (num < len) {
String s = data.substring(num, num + 2);
// System.out.println(s);
total += Integer.parseInt(s, 16);
num = num + 2;
}
/**
* 用256求余最大是255,即16进制的FF
*/
int mod = total % 256;
String hex = Integer.toHexString(mod);
len = hex.length();
// 如果不够校验位的长度,补0,这里用的是两位校验
if (len < 2) {
hex = "0" + hex;
}
return hex;
}
}