我使用 jpcap 教程中的一个简单程序。我想监听端口 4444 以检查我的其他客户端-服务器应用程序。我遇到了一个问题:方法 TCPPacket.getTCPData() 返回 byte[] 数组,限制为 30 个元素。我知道数据包包含超过 30 个字节的有用数据,不包括 TCP 标头字节。
如何获取超过 30 字节的数据包数据?
我检查了,方法 tcpPacket.getPayloadDataLength() 返回超过 500,而 TCPPacket.getTCPData() 返回一个 30 个字节的数组......为什么只有 30 个?
代码在这里
public class Test {
public static void main(String[] args) {
try {
Test test = new Test(PacketCapture.lookupDevices()[5].trim().split("\\s")[0]);
} catch(Exception e) {
e.printStackTrace();
}
}
public Test(String device) throws Exception {
// Initialize jpcap
PacketCapture pcap = new PacketCapture();
System.out.println("Using device '" + device + "'");
pcap.open(device, true);
pcap.setFilter("port 4444", true);
pcap.addPacketListener(new PacketHandler());
System.out.println("Capturing packets...");
pcap.capture(-1); // -1 is infinite capturing
}
}
class PacketHandler implements PacketListener {
BufferedOutputStream stream;
public PacketHandler() throws IOException {
Path path = Paths.get("out.txt");
stream = new BufferedOutputStream(
Files.newOutputStream(path, StandardOpenOption.CREATE, StandardOpenOption.APPEND));
}
public void packetArrived(Packet packet) {
try {
// only handle TCP packets
if(packet instanceof TCPPacket) {
TCPPacket tcpPacket = (TCPPacket)packet;
byte[] data;
data = tcpPacket.getTCPData();
stream.write(data);
stream.write("\r\n----------\r\n".getBytes());
stream.flush();
}
} catch( Exception e ) {
e.printStackTrace(System.out);
}
}
}