本文介绍了netbeans 7.1 IDE软件中Java程序中的网络嗅探器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package netwrksim;

/**
 *
 * @author DEBASISH
 */
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.DynamicTimeSeriesCollection;
import org.jfree.data.time.Second;
import org.jfree.data.xy.XYDataset;
import java.util.List;
import java.util.ArrayList;
import jpcap.*;
import jpcap.packet.*;
import java.util.*;
import java.awt.event.*;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.*;

public class Sniffer extends JPanel
{
    private static jpcap.NetworkInterface[] devices;
    private static int selecteddevice=-1;
    JFrame frame;
    public static JpcapCaptor captor;
    private static final String TITLE="Bandwidth Utilization Meter";;
public static List<packet> packets;
public static Timer timer;
   public static ArrayList<jradiobutton> radioButtonArray = new ArrayList<jradiobutton>();
private ButtonGroup group= new ButtonGroup();
    public static JButton  go;
    JFreeChart chart;
    static DynamicTimeSeriesCollection dataset;
    public Sniffer()
    {

         packets = new ArrayList<packet>();
         dataset =new DynamicTimeSeriesCollection(1,120, new Second());
         dataset.setTimeBase(new Second(0, 0, 0, 2, 1, 2011));
         dataset.addSeries(new float[0], 0, "PPP0 Bandwidth Utilization Meter");
         chart = createChart(dataset);
         getDevices();

        timer = new Timer(1000,new ActionListener(){
            public void actionPerformed(ActionEvent e)

            {    long tlen=0;
                List<packet> temp = new ArrayList<packet>(packets);
                packets.clear();
                for(Packet i : temp)
                {
                    tlen+=i.len;
                }
                float[] newData = new float[1];
                newData[0]=(float)tlen/1024;
                dataset.advanceTime();
                dataset.appendData(newData);
                }});
         setGUI();



    }


    void setGUI()
    {
        setLayout(new BorderLayout());
        frame = new JFrame();
        JPanel panel = new JPanel(new GridLayout(devices.length, 1));
        for (JRadioButton combo : radioButtonArray)
        {
            panel.add(combo);
        }
        JScrollPane scrollPane = new JScrollPane(panel);
        go= new JButton("GO!");
            go.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent ee)
                    {
   //problem starts here.............     
                        try{captor = JpcapCaptor.openDevice(devices[selecteddevice], 65535,true, 20);}catch(Exception e){}
                        timer.start();
                        captor.loopPacket(-1,new PacketPrinter());
     //.....................................


                    }
            }
            );

            go.setEnabled(false);

            panel.add(go);
            add(scrollPane, BorderLayout.CENTER);
            scrollPane.setSize(300,300);
            JFrame.setDefaultLookAndFeelDecorated(true);
            frame.setLayout(new GridLayout(2, 0));
            frame.add(scrollPane);
            frame.add(new ChartPanel(chart));
            frame.setSize(1024, 768);
          frame.setTitle("BW");
          frame.setVisible(true);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void getDevices()
    {
        devices = JpcapCaptor.getDeviceList();
        for(int i=0;i<devices.length;i++)>
        {
            String device=null;
            radioButtonArray.add(new JRadioButton());
            group.add(radioButtonArray.get(i));
            radioButtonArray.get(i).addActionListener(new RadioButtonListener());
            device= devices[i].name+" "+"("+devices[i].description+")";
            radioButtonArray.get(i).setText(device);
            }
    }
    public static void startSniffing() throws Exception
    {
        captor = JpcapCaptor.openDevice(devices[selecteddevice], 65535,true, 20);
    }
    public static void setSelectedDevice(int device)
    {
        selecteddevice = device;
    }

    public NetworkInterface getSelectedDevice()
    {
        if (selecteddevice == -1)
        {
            return null;
        }
        else
        {
            return devices[selecteddevice];
        }
    }
    public static void main(String args[])
    {
         Sniffer sniffer = new Sniffer();
         //sniffer.start();

    }
     private JFreeChart createChart(final XYDataset dataset) {
    final JFreeChart xyz = ChartFactory.createTimeSeriesChart(
        TITLE, "Time(Seconds)", "Bandwidth KB/s", dataset, true, true, false);
    final XYPlot plot = xyz.getXYPlot();
    ValueAxis domain = plot.getDomainAxis();
    domain.setAutoRange(true);
     ValueAxis range = plot.getRangeAxis();
    range.setRange(0,1000);
    return xyz;
}
}
 class RadioButtonListener extends JPanel implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        Sniffer.go.setEnabled(true);

        for (JRadioButton radio : Sniffer.radioButtonArray) {
            if (radio.isSelected()) {

                Sniffer.setSelectedDevice(Sniffer.radioButtonArray.indexOf(radio));

            }
        }
    }
}


class PacketPrinter implements PacketReceiver {
static long tlen;
public void receivePacket(Packet packet) {
  Sniffer.packets.add(packet);
  }
}


//程序无法正常工作!!!
//org.jfree.chart.ChartFrame中的错误
还有更多


//program is not working!!!!
// errors in org.jfree.chart.ChartFrame
and many more

推荐答案

public List<<b>Packet> packets;


这样,您很快就会发现自己尝试实例化某些对象而不是变量.


This way you will soon find out that you tried to instance some objects instead of variables.


这篇关于netbeans 7.1 IDE软件中Java程序中的网络嗅探器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 12:59