如何将jpcap数据包捕获数据显示给JTable?

时间:2016-04-14 17:41:32

标签: java swing jpcap

如何向Jtable显示数据。我有以下代码将jpcap数据包捕获数据显示给JTable。但它没有奏效。

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import net.sourceforge.jpcap.capture.PacketCapture;
import net.sourceforge.jpcap.capture.PacketListener;
import net.sourceforge.jpcap.net.ARPPacket;
import net.sourceforge.jpcap.net.ICMPPacket;
import net.sourceforge.jpcap.net.IPPacket;
import net.sourceforge.jpcap.net.Packet;
import net.sourceforge.jpcap.net.TCPPacket;
import net.sourceforge.jpcap.net.UDPPacket;

public class NetworkStatistics implements PacketListener{

public JFrame frmNmcidsNetworkStatistics;
private Vector<String> row;
    private Vector<Vector> rowData;
    private Vector<String> columnNames;
    private DefaultTableModel DFMO;
    private JTable table;
    private JScrollPane scrollPane;
    private PacketCapture captureobj;
private String filter = "";
private int totalpackets = -1;

private static int counter = 0;

/*
 * Create the application.
*/
public NetworkStatistics() {
    initialize();
}

/*
 * Initialize the contents of the frame.
*/
private void initialize() {
    frmNmcidsNetworkStatistics = new JFrame();
    frmNmcidsNetworkStatistics.setTitle("NMCIDS: Network Statistics");
    frmNmcidsNetworkStatistics.setBounds(100, 100, 450, 300);
    frmNmcidsNetworkStatistics.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    row = new Vector<String>();
        rowData = new Vector<Vector>();

        columnNames = new Vector<String>();
        columnNames.addElement("Sr. No");
        columnNames.addElement("Source IP");
        columnNames.addElement("Source MAC");
        columnNames.addElement("Destination IP");
        columnNames.addElement("Destination MAC");
        columnNames.addElement("Protocol");

        DFMO = new DefaultTableModel(rowData, columnNames);
        table = new JTable(DFMO);
        scrollPane = new JScrollPane(table);
        frmNmcidsNetworkStatistics.getContentPane().add(scrollPane, BorderLayout.NORTH);

}

public void GetNetworkPackets(String capdev){
    try{
        captureobj = new PacketCapture();
        captureobj.open(capdev, true);
        captureobj.setFilter(filter, true);
        captureobj.addPacketListener(this);
        captureobj.capture(totalpackets);
    }
    catch(Exception e){
        e.printStackTrace();
    }
}

public void dispalyPacketsTable(int srno, String sip, String smac, String dip, String dmac, String proto){
    row.addElement(""+srno);
        row.addElement(sip);
        row.addElement(smac);
        row.addElement(dip);
        row.addElement(dmac);
        row.addElement(proto);
        rowData.addElement(row);
        DFMO.fireTableDataChanged();
}

@Override
public void packetArrived(Packet pdata) {
     counter++;
     printdata(pdata);
}

public void printdata(Packet pdata){
    if(pdata instanceof TCPPacket){
            TCPPacket TCPO = (TCPPacket) pdata;
            dispalyPacketsTable(counter, TCPO.getSourceAddress(), TCPO.getSourceHwAddress(), TCPO.getDestinationAddress(), TCPO.getDestinationHwAddress(), "TCP");
        }
}
}

运行此代码时程序挂起。但是,当我想将数据包捕获的数据显示到命令promt时,它会正确显示。请帮我解决这个问题。提前谢谢。

1 个答案:

答案 0 :(得分:2)

  

当我运行此代码时程序挂起

可能是因为您的代码在Event Dispatch Thread(EDT)上运行,导致GUI无法响应事件。

您的数据包侦听器需要在单独的线程上运行,因此您不会阻止EDT。阅读Concurrency in Swing上的Swing教程中的部分。我猜您应该使用SwingWorker,并且可以publish结果显示结果。