⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 packetcount.java

📁 完成的主要是从网络采集数据包之后存入数据库中然后再统计
💻 JAVA
字号:
import java.sql.*;

public class PacketCount
{  
	public static void main(String []args) throws Exception 
    {  int len40=0,lenid0=0,count=0,packlen; 	
	   int total=0,tcplen=0,udplen=0,icmplen=0,protrol;
       
	   String JDriver="sun.jdbc.odbc.JdbcOdbcDriver";    //声明JDBC驱动程序对象
       String conURL="jdbc:odbc:JavaPackets";        //定义JDBC的URL对象
       
       try
       {  Class.forName(JDriver);    }          //注册驱动程序
       catch(java.lang.ClassNotFoundException e)
       {  System.out.println("ForName:"+e.getMessage());   }
       
       try
       {  Connection con=DriverManager.getConnection(conURL);    //建立数据库连接
          String str="select * from CPacket";    //从数据库中的CPacket表中进行操作
          Statement s=con.createStatement();     //创建Statement类对象s,准备进行SQL操作
          ResultSet rs=s.executeQuery(str);      //执行SQL查询操作
          while(rs.next())
          {  protrol=rs.getInt("高层协议");    //得到表中的 高层协议 字段的数据
             packlen=rs.getInt("长度");        //得到表中的 长度(包的长度) 字段的数据
             if(protrol==6)                   //由于每个字段只允许读一次,故用一个变量保存起来
                tcplen+=packlen;
             if(protrol==17)
                udplen+=packlen;
             if(protrol==1)
                icmplen+=packlen;

             if(packlen==40)
             {
                len40++;
                if(rs.getInt("标识符")==0)
                  lenid0++;
             }
             count++;
   	         total+=packlen;
   	      }
          s.close();
          con.close();
       }
       catch(SQLException e)
       {
       	System.out.println("SQLException:"+e.getMessage());
       } 
       
       System.out.println("\n**********************************************");
       System.out.println("以下是协议统计的结果:\n");  
       System.out.println("数据总数:"+total);
       System.out.println("The tcp is:"+tcplen+"\t"+"占 "+(100.0*tcplen/total)+"%");
       System.out.println("The udp is:"+udplen+"\t"+"占 "+(100.0*udplen/total)+"%");
       System.out.println("The icmp is:"+icmplen+"\t"+"占 "+(100.0*icmplen/total)+"%");
       System.out.println("**********************************************\n");
       System.out.println("\n**********************************************");
       System.out.println("以下是包长统计的结果:\n");  
       System.out.println("平均包长:"+total/145381+"\r");
       System.out.println("数据总数 count:"+count+"\r");
       System.out.println("length:"+"\t40"+"\twei:"+"\t"+len40+"\r");
       System.out.println("包长为40,id为0的个数为"+lenid0+"\t占:"+100.0*lenid0/145381+"%"+"\r");
       Connection con=DriverManager.getConnection(conURL);    //建立数据库连接
       for(int i=41;i<1501;++i)
       {   
    	   String str="select * from CPacket where 长度='"+i+"'";
    	   PreparedStatement s=con.prepareStatement(str,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);   
           ResultSet rs=s.executeQuery();                   //上面那句话相当于 Statement s=con.createStatement();这句,
       	   rs.last();                                       //创建Statement类对象s,准备进行SQL操作. 
       	   System.out.println("length:\t"+i+"\twei:\t"+rs.getRow()+"\r\n");
       }
       con.close(); 
       System.out.println("**********************************************\n");
    }
}

⌨️ 快捷键说明

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