我正在编写一个通过UDP端口161连接到网络服务器的snmp客户端。它将发送一个ASN格式的消息,并将从服务器接收响应。我已经尝试了所有我能想到的东西,但我似乎无法让它正常工作。
该程序应该接收并解析cmd中以下命令返回的计数器字段:
snmpget -d -v 2c -c public 129.130.10.43 ip.ipInReceives.0
//命令结果:
{
向UDP发送43个字节:[129.130.10.43]:161-> [0.0.0.0]
0000:30 29 02 01 01 04 06 70 75 62 6C 69 63 A0 1C 02 0)..... public ...
0016:04 28 6A DF 1F 02 01 00 02 01 00 30 0E 30 0C 06。(j ........ 0.0 ..
0032:08 2B 06 01 02 01 04 03 00 05 00。+ .........
从UDP收到47个字节:[129.130.10.43]:161-> [0.0.0.0]
0000:30 2D 02 01 01 04 06 70 75 62 6C 69 63 A2 20 02 0 -..... public。
0016:04 28 6A DF 1F 02 01 00 02 01 00 30 12 30 10 06。(j ........ 0.0 ..
0032:08 2B 06 01 02 01 04 03 00 41 04 1B 49 0C 95。+ ....... A..I ..
IP-MIB :: ipInReceives.0 = Counter32:457772181
}
//我的asn消息格式化注释:
十六进制值保存在byte []数组中并传递给udp服务器:
30 29 02 01 01 04 06 70 75 62 6C 69 63 A0 1C 02
04 XX XX XX XX 02 01 00 02 01 00 30 0E 30 0C 06
08 2B 06 01 02 01 04 03 00 05 00
XX表示请求ID 对于每个请求,XX将被更改(可能是随机的),以便创建唯一的请求ID
代码:
public static void snmpMessage() throws SocketException
{
DatagramSocket socket = null;
byte[] serverResponse = new byte[1024];
InetAddress addy = null;
byte[] hex = hexStringToByteArray("302902010004067075626C6963A01C0204121533EA020100020100300E300C06082B060102010403000500"); //note: this hex string is the exact string from the example program, so I know the string is correct
addy = InetAddress.getByName("129.130.10.48"); //server IP address
socket= new DatagramSocket();
//method that creates unique snmp asn message replacing the XX's with appropriate non-negative hex values
hex = messageGenerator();
//Data reproduction from byte[] array
System.out.println("byte array integer format:\n" + Arrays.toString(hex)); //correctly prints out integer values
System.out.println("\nbyte array length: " + hex.length); //Correctly prints length of sending array (43)
System.out.println("\nserver name: " + addy.getCanonicalHostName()); //correctly prints server name
String hex2 = toHex(hex);
System.out.println("\nbyte array in hex format: \n" + hex2); //correctly reproduces original hex string from byte[] array
//at this point, I can only assume that my data is stored correctly
//send byte array to server
DatagramPacket sendPacket = new DatagramPacket(hex, hex.length, addy, 161);
socket.send(sendPacket);
//get server's response
DatagramPacket receivePacket = new DatagramPacket(serverResponse, serverResponse.length);
System.out.println("\nWaiting for server response...\n"); //prints out
socket.receive(receivePacket);
System.out.println("Response received"); //does not print
}
我的教授发布了一个示例,他只是将指针传递给char字符串: sendto(sockfd,(char *)s,43,0,(struct sockaddr *)& serv_addr,servlen); 他传递相同十六进制代码的代码完美无缺。
我唯一能想到的是我没有正确地格式化,发送或收听,但是在倾注了文档和示例之后,我无法解决任何问题。有人有指点吗?
答案 0 :(得分:0)
如果您将问题范围缩小到十六进制字符串到字节数组转换,请转到:
使用Java
将十六进制字符串转换为字节数组public static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
return data;
}
答案 1 :(得分:0)
我明白了。我用来将我的十六进制字符串转换为byte []的方法不能正常工作。我切换到使用内置转换器“DatatypeConverter.parseHexBinary(str);”它运作得很好。