Skip to content

Instantly share code, notes, and snippets.

@blackraccoon000
Created January 31, 2011 03:26
Show Gist options
  • Save blackraccoon000/803586 to your computer and use it in GitHub Desktop.
Save blackraccoon000/803586 to your computer and use it in GitHub Desktop.
PacketCapture2
package combotest;
import jpcap.JpcapCaptor;
import jpcap.NetworkInterface;
import jpcap.NetworkInterfaceAddress;
import jpcap.PacketReceiver;
import jpcap.packet.Packet;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
/**
* Created by IntelliJ IDEA.
* User: white
* Date: 11/01/28
* Time: 8:03
* To change this template use File | Settings | File Templates.
*/
public class ComboBoxD extends JFrame implements ItemListener,PacketReceiver {
DefaultComboBoxModel model;
JComboBox combo;
static JTextArea area,area2,area3;
public static void main(String args []){
ComboBoxD frame = new ComboBoxD();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(10,10,400,200);
frame.setTitle("Title");
frame.setVisible(true);
}
ComboBoxD(){
NetworkInterface device[] = JpcapCaptor.getDeviceList();{
String deviceName [] = new String[device.length];
for(int i=0;i<device.length;i++){
deviceName[i]=(i +": "+ device[i].description);
}
model = new DefaultComboBoxModel(deviceName);
combo = new JComboBox(model);
combo.setPreferredSize(new Dimension(300,30));
combo.addItemListener(this);
JPanel p = new JPanel();
p.add(new JLabel("DeviceList"));
p.add(combo);
area3 = new JTextArea(10,10);
JScrollPane sp = new JScrollPane(area3);
area = new JTextArea();
area2 = new JTextArea(1,1);
JPanel pArea = new JPanel();
pArea.add(area2);
pArea.add(sp);
getContentPane().add(p, BorderLayout.SOUTH);
getContentPane().add(pArea,BorderLayout.NORTH);
}
}
public void itemStateChanged(ItemEvent e) {//ComboBoxからのイベント
if(e.getStateChange() == ItemEvent.SELECTED){
int index = combo.getSelectedIndex();//選択したコンボボックスの番号を表示
area.setText(String.valueOf(Integer.parseInt(String.valueOf(index))));//areaに番号を書き込む
devices();//areaから番号を受け、デバイス情報の表記
Thread thread = new Thread(){
public void run(){
try{
cap(new String[]{area.getText()});
}catch(Exception ex){
ex.printStackTrace();
}
}
}; thread.start();
}
}
public static void cap(String [] args) throws Exception {//選択されたデバイスからのパケットを受け取る
NetworkInterface[] devices = JpcapCaptor.getDeviceList();
JpcapCaptor jpcap = JpcapCaptor.openDevice(devices[Integer.parseInt(area.getText())], 2000, false, 20);
jpcap.loopPacket(-1, new ComboBoxD());
}
public void receivePacket(Packet packet) {//流れているパケットをSystemOutする
area3.append(packet.toString());
System.out.println(packet);
}
public void devices() { //選択したデバイス情報を表示
NetworkInterface[] devices = JpcapCaptor.getDeviceList(); {
area2.append(devices[Integer.parseInt(area.getText())].description);
for (NetworkInterfaceAddress a : devices[Integer.parseInt(area.getText())].addresses){
area2.append("\nIPアドレス: " + a.address + "\nサブネットマスク:" + a.subnet + "\nブロードキャストアドレス:"
+ a.broadcast + "\n");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment