Hi.

im working on a tcp sniffer project. at first, ihave created the system using JCreator and it works fine without any interface. Next, im trying to create the user interface for it using NetBean 7.1. of course i have to break the code into parts and pieces to be fitted into the button function. It seems that when i run the program, it doesn't produce any output but keeps on running till manually stopped. it happen when i tried to insert the 'open device' code. another part that i suspected to cause the problem is 'Thread.sleep(25);' part..Before inserting that part, i tried to run and it produces the output which is the frame, button, etc..

I have attached the program along with it. badly need any help that i can get from here.

thanks


import java.util.ArrayList;
import jpcap.JpcapCaptor;
import jpcap.NetworkInterface;
import jpcap.PacketReceiver;
import jpcap.packet.Packet;
import jpcap.packet.TCPPacket;

public class NewJFrame extends javax.swing.JFrame {

NetworkInterface[] devices = JpcapCaptor.getDeviceList();
static int deviceNum=0;
JpcapCaptor captor;
PacketStorage ps;

public NewJFrame() {
initComponents();
for(int i=0;i<devices.length;i++)
{
jComboBox1.addItem(""+devices[i].name);
}
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jPanel1 = new javax.swing.JPanel();
jButton2 = new javax.swing.JButton();
jComboBox1 = new javax.swing.JComboBox();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jMenuItem3 = new javax.swing.JMenuItem();
jMenuItem2 = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
jMenu3 = new javax.swing.JMenu();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);

jButton2.setText("Stop");

jComboBox1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jComboBox1ActionPerformed(evt);
}
});

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jComboBox1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2)
.addGap(22, 22, 22))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton2))
.addGap(27, 27, 27))
);

jMenu1.setText("File");

jMenuItem1.setText("Run");
jMenu1.add(jMenuItem1);

jMenuItem3.setText("Stop");
jMenu1.add(jMenuItem3);

jMenuItem2.setText("Exit");
jMenu1.add(jMenuItem2);

jMenuBar1.add(jMenu1);

jMenu2.setText("Edit");
jMenuBar1.add(jMenu2);

jMenu3.setText("About");
jMenuBar1.add(jMenu3);

setJMenuBar(jMenuBar1);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 602, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 253, Short.MAX_VALUE)
.addContainerGap())
);

pack();
}// </editor-fold>

private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
deviceNum = jComboBox1.getSelectedIndex();
try{
captor=JpcapCaptor.openDevice(devices[deviceNum], 65535, true , 20);
}
catch(Exception ex1){System.out.println("Cannot open device");}
ps = new PacketStorage();
new Thread(new Parser(ps)).start();

captor.loopPacket(-1, ps);
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/*
* Set the Nimbus look and feel
*/
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/*
* If Nimbus (introduced in Java SE 6) is not available, stay with the
* default look and feel. For details see
* http://download.oracle.com/javase/tu...feel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {
new NewJFrame().setVisible(true);
}
});
}

private static class PacketStorage implements PacketReceiver
{
private ArrayList<Packet> packets = new ArrayList<>();

public void receivePacket(Packet p)
{
packets.add(p);
}
public Packet getPacket()
{
return packets.size()>0?packets.remove(0):null;
}
}

private static class Parser implements Runnable
{
PacketStorage ps1;
public Parser(PacketStorage ps2)
{
this.ps1=ps2;
}


public void run()
{
Packet p;
TCPPacket tcp;

while(true)
{
p=ps1.getPacket();
if(p!=null && p instanceof TCPPacket )
{
tcp =(TCPPacket)p;
jTextArea1.append("Src: " + tcp.src_ip + ":" + tcp.src_port + " Dst: " + tcp.dst_ip + ":" + tcp.dst_port+"\n");
jTextArea1.append("Sequence Number Value: " + tcp.sequence + " Ack Number: " + tcp.ack_num+"\n");

}

try{
Thread.sleep(25);
}
catch(InterruptedException e){}

}
}
}
// Variables declaration - do not modify
private javax.swing.JButton jButton2;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenu jMenu3;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JMenuItem jMenuItem3;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private static javax.swing.JTextArea jTextArea1;
// End of variables declaration
}