私はまったく新しいものJNetPcap
で、まだそれを使って自分の道を見つけています.私のプロジェクト用にパケットスニファーを構築しようとしてJTextArea
いpcap.loop()
ます.を使用していますが、特定の整数値を使用して最初のパラメーターを設定すると、pcap.loop()
キャプチャされた 5 つのパケットが出力されるとします。停止ボタンを押すまで、パケットを継続的にキャプチャして出力する必要があります。以下の構文は、Packet ハンドラーを示しています。
PcapPacketHandler<String> jpacketHandler = new PcapPacketHandler<String>() {
public void nextPacket(PcapPacket packet, String user) {
// System.out.printf is included to check if my code works in a non GUI fashion
System.out.printf("Received packet at %s caplen=%-4d len=%-4d %s\n",
new Date(packet.getCaptureHeader().timestampInMillis()),
packet.getCaptureHeader().caplen(), // Length actually captured
packet.getCaptureHeader().wirelen(), // Original length
user // User supplied object
);
Date a = new Date(packet.getCaptureHeader().timestampInMillis());
int b = packet.getCaptureHeader().caplen();
int c = packet.getCaptureHeader().wirelen();
String d = user;
pcktTextArea.append("Received packet at " + a + " caplen=" + Integer.toString(b) + " len=" + Integer.toString(b) + user + "\n" );
pcktTextArea.setForeground(Color.red);
pcktTextArea.setFont(font);
}
};
append
ここにあるのは、テキストエリアの情報を出力するために使用する pcktTextArea です。
pcktTextArea.append("Received packet at " + a + " caplen=" + Integer.toString(b) + " len=" + Integer.toString(b) + user + "\n" );
pcktTextArea.setForeground(Color.red);
pcktTextArea.setFont(font);
最後に、Pcap.loop
私が問題を抱えているのは、i を 5 に置き換えると、に出力されますJTextArea
が、入れるとPcap.LOOP_INFINTE
コンソールからのみ情報が出力され、GUI JTextArea には出力されません。
int i = Pcap.LOOP_INFINITE;
pcap.loop(i , jpacketHandler, " ");
/***************************************************************************
* Last thing to do is close the pcap handle
**************************************************************************/
pcap.close();
Textarea に情報を出力する前にループを終了する必要があるためですか?