关闭

ip统计

标签: cnullfile网络
660人阅读 评论(0) 收藏 举报
分类:

//以下是在网络中心时,中心让做的一个ip统计程序,要求对一个数据包中的32G的数据进行统计,实验证明,这个程序是相当的快,总共运行了4个小时就完成了

#include <stdio.h>

#include <Winsock2.h>

#include <memory.h>   //memset()函数所需的头文件

#include "time.h"

#include "windows.h"

 

int ipa[8192];//a59.70.128.0--------59.70.159.255       32C

int ipb[8192];//b115.158.64.0-----115.158.95.255       32C

int ipc[4096];//c202.196.0.0------202.196.15.255       16C

int ipd[64];//d218.28.23.128----218.28.23.191       64个(网通

int ipe[64];//e218.28.242.192---218.28.242.255      64个(网通)

int ipf[128];//f218.29.115.0---218.29.115.127       128个(网通)

int ipg[4096];//g218.198.112.0----218.198.127.255      16C

 

int a=0;

int b=0;

int c=0;

int d=0;

int e=0;

int f=0;

int g=0;

      

 

 

 

int yys;

int yye;

FILE *pXml;

 

 

int iAToB(int a[],unsigned long temp,unsigned long tempA,unsigned long tempB)

{

       if(tempA<=temp && temp<=tempB){

              a[temp-tempA]=1;

              return 1;

       }else{

              return 0;

       }

}

 

void setFlag(unsigned long temp)

{    

   if(iAToB(ipa,temp,ntohl(inet_addr("59.70.128.0")),ntohl(inet_addr("59.70.159.255"))))

   {

   }else if(iAToB(ipb,temp,ntohl(inet_addr("115.158.64.0")),ntohl(inet_addr("115.158.95.255")))){

   }else if(iAToB(ipc,temp,ntohl(inet_addr("202.196.0.0")),ntohl(inet_addr("202.196.15.255")))){

   }else if(iAToB(ipd,temp,ntohl(inet_addr("218.28.23.128")),ntohl(inet_addr("218.28.23.191")))){

   }else if(iAToB(ipe,temp,ntohl(inet_addr("218.28.242.192")),ntohl(inet_addr("218.28.242.255")))){

   }else if(iAToB(ipf,temp,ntohl(inet_addr("218.29.115.0")),ntohl(inet_addr("218.29.115.127")))){

   }else if(iAToB(ipg,temp,ntohl(inet_addr("218.198.112.0")),ntohl(inet_addr("218.198.127.255")))){

   }

}

int cntArry(int a[],int k)

{

       int cnt=0;

       for(int i=0;i<k;i++)

       {

              if(a[i])

                     cnt++;

       }

       return cnt;

}

 

void show()

{

          a=cntArry(ipa,8192);

          b=cntArry(ipb,8192);

          c=cntArry(ipc,4096);

          d=cntArry(ipd,64);

          e=cntArry(ipe,64);

          f=cntArry(ipf,128);

          g=cntArry(ipg,4096);

          printf("@已累计不同IP a=%d,b=%d,c=%d,d=%d,e=%d,f=%d,g=%d,/ttotel:%d/n",a,b,c,d,e,f,g,(a+b+c+d+e+f+g));

          fprintf(pXml,"@已累计不同IP a=%d,b=%d,c=%d,d=%d,e=%d,f=%d,g=%d,/ttotel:%d/n",a,b,c,d,e,f,g,(a+b+c+d+e+f+g));

}

 

void main()

{

 

   memset(ipa,0,8192*sizeof(int));  //数组内存空间初始化

   memset(ipb,0,8192*sizeof(int));

   memset(ipc,0,4096*sizeof(int));

   memset(ipd,0,64*sizeof(int));

   memset(ipe,0,64*sizeof(int));

   memset(ipf,0,128*sizeof(int));

   memset(ipg,0,4096*sizeof(int));

  // printf("%u/n",inet_addr("0.0.0.1")) ;

  // printf("%u/n",ntohl(inet_addr("0.0.0.1"))) ;

        long start,end;

 

      

 

 

     FILE *fpin;//源数据文件

       char cTemp;

       printf("******欢迎你使用日志IP统计软件(V1.0*****/n/n");

       printf("请输入@开始@(xx两位):/n");

       scanf("%d",&yys);

       printf("请输入#结束#(xx两位):/n");

       scanf("%d",&yye);

       char time[100]={'d',':','/','/','N','E','4','0','-','l','o','g',

                         '/', '1','9','2','.','1','6','8','.','6','1','.','2','5','4',

                               '/','1','0','-','1','1','-','0','8',

                               '/','(','0','0',')','.','l','o','g','/0'};

 

       //               1 2   3  4 5  6  7  8  9  10  11  12

       int mmDays[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};

       int allsFlag=1;

   start = clock();        //开始时间

       for(int yyt=yys;yyt<yye;yyt++){

       time[28]='0'+yyt/10;   //年数的高位

          time[29]='0'+yyt%10;   //年数的低位

         for(int mmt=1;mmt<=12;mmt++){

          time[31]='0'+mmt/10;  //月数的高位

             time[32]='0'+mmt%10;   //月数的低位

                for(int ddt=1;ddt<=mmDays[mmt];ddt++){

             time[34]='0'+ddt/10;    //天数的高位

                time[35]='0'+ddt%10;    //天数的低位

                      for(int hht=0;hht<=23;hht++){   //这个7是不是有点问题呀?(************************************)

                   time[38]='0'+hht/10;   //小时的十位

                      time[39]='0'+hht%10;   //小时的个位

                                   ///////////////////////////////打开文件  开始

                                   //if((fpin=fopen("F://NE40-log/192.168.61.254/10-11-08/(00).log","r"))==NULL)

                               

                                   if((fpin=fopen(time,"r"))==NULL)

                                   {

                            //            fclose(fpin);

                            //            printf("@@@:%s文件/n",time);

                                   //     hht++;

                                          continue;

                                   }

                                   pXml = fopen("d://cnt.txt", "a");//注意文件路径

                                   show();

                                   printf("#正在统计:%s文件/n",time);

                                   fprintf(pXml,"#正在统计:%s文件/n",time);

                                   ///////////////////////////////打开文件  结束

                                  

                                   //////////////////////////////////////////得到文件第一列的IP地址

                                   if (allsFlag)

                                   {

                                          char ipStrF[16];

                                          for(int i=0;i<30;i++)

                                          {

                                                 cTemp=fgetc(fpin);

                                                 if(cTemp==' ')

                                                 {

                                                        ipStrF[i]='/0';

                                                        setFlag(ntohl(inet_addr(ipStrF)));

                                                        break;

                                                 }

                                                 ipStrF[i]=cTemp;

                                          }

                                          allsFlag=0;

                                   }else

                                   {

                                              char ipStr[16];

                                                 for(int i=0;i<30;i++)

                                                 {

                                                        cTemp=fgetc(fpin);

                                                        if(cTemp==' ')

                                                        {

                                                               ipStr[i]='/0';

                                                               setFlag(ntohl(inet_addr(ipStr)));

                                                               break;

                                                        }

                                                        ipStr[i]=cTemp;

                                                 }

 

                                   }

                                  

                                   while((cTemp=fgetc(fpin))!=EOF)

                                   {

                                          if(cTemp=='/n'){

                                                 char ipStr[16];

                                                 for(int i=0;i<30;i++)

                                                 {

                                                        cTemp=fgetc(fpin);

                                                        if(cTemp==' ')

                                                        {

                                                               ipStr[i]='/0';

                                                               setFlag(ntohl(inet_addr(ipStr)));

                                                               break;

                                                        }

                                                        ipStr[i]=cTemp;

                                                 }

                                          }

                                   }

                                   //////////////////////////////////////////