我需要以以下格式发送 UDP 数据包:
[1 个八位字节][2 个八位字节][3 个八位字节][4 个八位字节][5 个短字节]
例如:
77.125.65.201:27015
十六进制:
4D 7D 41 C9 69 87
这是我用wireshark捕获的:
为什么是 2 个额外的 OCTET [00, 00] ?
这就是我的格式:
byte[] responseHeader = { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF, 0x66, (byte)0x0A };
byte[] testIP = getByteIp("77.125.65.201:27015");
byte[] response = new byte[responseHeader.length + testIP.length];
System.arraycopy(responseHeader, 0, response, 0, responseHeader.length);
System.arraycopy(testIP, 0, response, responseHeader.length, testIP.length);
private byte[] getByteIp(String fullData){
String[] data = fullData.split(":");
byte[] returnArray = new byte[8];
byte[] ip = new byte[4];
try {
ip = InetAddress.getByName(data[0]).getAddress();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] port = new byte[2];
port = ByteBuffer.allocate(4).putInt(Integer.parseInt(data[1])).array();
System.arraycopy(ip, 0, returnArray, 0, ip.length);
System.arraycopy(port, 0, returnArray, ip.length, port.length);
return returnArray;
}