www.pudn.com > jpcap.dll-jpcap.jar.zip > JpcapTip.java, change:2009-05-28,size:4052b


 
import java.io.*; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.SQLException; 
import java.sql.Statement; 
import java.util.Date; 
 
import javax.swing.JOptionPane; 
 
 
import jpcap.*; 
public class JpcapTip implements JpcapHandler { 
      static int j=0; 
      static long p=0,q=0; 
      IPPacket ip; 
      String s,s1; 
      Long s2; 
     static long time11,time12; 
	String time;      
     	static long num=0; //加一个静态产量,表示抓的包的序号,不然连自己抓的顺序都不知道 
     	public static Connection conn; 
      public void handlePacket(Packet packet){        
          if(packet instanceof IPPacket)//判断是不是是ip包 
              {  
              System.out.println(j+"ok"); 
              ip=(IPPacket)packet ; 
               if(j<10000) 
              {  
            	 /*   try { 
            		      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
            		      conn = DriverManager.getConnection("jdbc:odbc:Jpcap"); 
            		      //JOptionPane.showMessageDialog(null, "连接数据库!"); 
            		       
            		    } 
            		    catch (Exception e) { 
            		      e.printStackTrace(); 
            		     // JOptionPane.showMessageDialog(null, "连接数据库错误!"); 
            		      JOptionPane.showConfirmDialog(null, "连接数据库错误", "抓包",JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE); 
            		    } 
            		    Date jhsjDate = new Date(); 
						//In_Time.setText(jhsjDate.toLocaleString()); 
            		    time = jhsjDate.toLocaleString(); 
            		    insert("INSERT [JpcapTip] ([src_ip] , [dst_ip] , [protocol] , [length] , [version] , [ident] , [rsv_frag] , [offset] , [hop_limit] , [rsv_tos] , [time] ) values('"+ ip.src_ip  
								+ "','" + ip.dst_ip + "','" + ip.protocol 
								+ "','" + ip.length + "','" + ip.version 
								+ "','" + ip.ident + "','" + ip.rsv_frag 
								+ "','" + ip.offset + "','" + ip.hop_limit 
								+ "','" + ip.rsv_tos + "','" + time + "')"); 
					*/ 
            	              
					try  { 
     	      RandomAccessFile rf = new RandomAccessFile("packet.txt", "rw");//动态文件 
                      rf.seek(rf.length());//文件尾 
                      rf.writeBytes(num+"\t"+ip.src_ip+"\t"+ip.dst_ip+"\t"+ip.protocol+"\t"+ 
                      ip.length+"\t"+ip.version+"\t"+ip.ident+"\t"+ 
                      ip.rsv_frag+"\t"+ip.offset+"\t"+ip.hop_limit+"\t"+ 
                      ip.rsv_tos+"\r\n");//写入 
                      rf.close();//关闭文件 
                      num++; 
                 } 
                 catch (Exception e) { 
                      e.printStackTrace(); 
                 } 
                 j++;} 
                    else  
               { 
                time12=System.currentTimeMillis();//当前系统时间 
                System.out.println(""+time12+"-"+time11+"="+(time12-time11)+"\ncapture count:"+j); 
              	System.exit(0); 
              } 
          } 
           } 
public static void main(String[] args) throws java.io.IOException 
      { 
      	  
          String[] devices = Jpcap.getDeviceList(); 
          //String deviceName = devices[0]//这是课本上的设置 
          String deviceName = devices[1];//这个数字原本为0,经过调试发现0根本没有结果,后觉得和我的网卡插槽有关,所以改了一个1后,发现结果是大大的有啊 
          Jpcap jpcap = Jpcap.openDevice(deviceName, 1028, true, 100); 
            //deviceName即将打开的设备名// 
            //1028从设备上一次读取的最大字节数// 
            //true说明是否将设备设为混杂模式的Boolean值// 
            //100超时值// 
          time11=System.currentTimeMillis(); 
          while(true) 
          { 
          jpcap.loopPacket(-1, new JpcapTip()); 
          }                     
      }      
 
public static boolean insert(String sql) { 
	boolean result = false; 
	try { 
		Statement stmt = conn.createStatement(); 
		result = stmt.execute(sql); 
	} catch (SQLException e) { 
		e.printStackTrace(); 
	} 
	return result; 
}//插入 
 
 
 
 
}