jpcaptip.java

来自「这个程序实现了在网络上截获邮件数据并还原邮件。仅供学习参考」· Java 代码 · 共 61 行

JAVA
61
字号
import java.io.*;
import jpcap.*;
import java.util.*;

public class JpcapTip implements JpcapHandler {
      static int j=0;
      static long p=0;
      static StringBuffer sting=new StringBuffer("");
      
      public void handlePacket(Packet packet){
          IPPacket ip;
          if(packet instanceof IPPacket)
          { 
            j++;
            ip=(IPPacket)packet;
            sting.append(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");
                	         
           } 
      }

      public static void main(String[] args) throws java.io.IOException
      {
      	  String[] devices = Jpcap.getDeviceList();
          String deviceName = devices[0];
          Jpcap jpcap = Jpcap.openDevice(deviceName, 1028, true, 100);
          System.out.println("Start capturing on "+devices[0]);
          while(true)
          {
          jpcap.loopPacket(200000, new JpcapTip());
          chuli();}
      }
     
     public static void chuli()
     {
     	  long time11=System.currentTimeMillis();
          
          try {
     		String aa="packe"+p+".txt";
     		RandomAccessFile rf = new RandomAccessFile(aa, "rw");
            rf.seek(rf.length());
            rf.writeBytes(sting.toString());
            rf.close();
          }
          catch (Exception e) {
            e.printStackTrace();
          }
          long time12=System.currentTimeMillis();
          System.out.println(""+time12+"-"+time11+"="+(time12-time11)+"\ncaptrue count:"+j);
          
          sting.delete(0,sting.length()-1);
          j=0;
          p++;
          System.out.println("the txt:"+p);
     }
} 

 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?