gpt4 book ai didi

java - 如何将jpcap数据包捕获的数据显示到JTable?

转载 作者:行者123 更新时间:2023-12-02 03:43:05 25 4
gpt4 key购买 nike

如何将数据显示到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");
}
}
}

当我运行此代码时,程序挂起。但是,当我想显示数据包捕获的数据以命令提示时,它会正确显示。请帮助我摆脱这个困境。预先感谢您。

最佳答案

The program hangs when i run this code

可能是因为您的代码在事件调度线程 (EDT) 上运行,这会阻止 GUI 响应事件。

您的数据包监听器需要在单独的线程上运行,这样您就不会阻塞 EDT。阅读 Swing 教程中关于 Concurrency in Swing 的部分。我猜您应该使用 SwingWorker 并且您可以在结果可用时发布结果。

关于java - 如何将jpcap数据包捕获的数据显示到JTable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36630280/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com