Flow Speed Meter

Source: Internet
Author: User

Thanks to forget because of you, I will not fall into the trap, I will forget the people, once the feelings, despite the things, not afraid to forget, afraid to remember something should not be remembered. Life is always such a water time. #include <stdio.h> #include <ctime> #include  <winsock2.h> #pragma  comment (lib, "ws2_32. Lib ") #define  icmp_echo 8#define packdatasize 1000ushort checksum (USHORT* buff, Int size) { unsigned long cksum=0; // while (size>1)  {  cksum+=* Buff++;  size -=sizeof (USHORT);  } // if (size)  {  cksum+=* (UCHAR*) Buff; } // cksum= (cksum >> 16) + (CKSUM&NBSP;&AMP;&NBSP;0XFFFF);  cksum+= ( CKSUM&NBSP;&GT;&GT;&NBSP;16); return  (USHORT) (~cksum);} Bool settimeout (SOCKET&NBSP;S,&NBSP;INT&NBSP;NTIME,&NBSP;BOOL&NBSP;BRECV) { int ret =  :: setsockopt (s, sol_socket,   brecv ? so_rcvtimeo : so_sndtimeo,  (char*) &ntime, sizeof (ntime));  return ret != socket_error;} typedef struct _ipheader  // 20 bytes of IP header {    uchar      iphverlen;      //  version number and head length (4 bits each)     UCHAR      ipTOS;          //  Service Type       USHORT    ipLength;       //  Total packet length, that is, the length of the entire IP report     ushort    ipid;     //   Packet identification, the unique identity of each number sent by the report     USHORT    ipFlags;        //  logo     UCHAR     ipTTL;        //  time to Live is ttl    uchar      ipprotocol;     //  protocol, possibly TCP, UDP, ICMP, etc.     USHORT     ipchecksum;     //  Checksum     ULONG     ipSource;        //  Source IP Address     ULONG      ipdestination;  //  Destination IP Address} ipheader, *pipheader;typedef struct icmp_hdr{     unsigned char   icmp_type;  //  Message Types      unsigned char   icmp_code;  //  Code      unsigned short  icmp_checksum; //  checksum  //  Below is the echo header      The ID number used by unsigned short  icmp_id;  //  to uniquely identify this request, usually set to process id     unsigned short  icmp_sequence; //  Serial Number     unsigned long    icmp_timestamp; //  timestamp} icmp_hdr, *picmp_hdr;//thread function declaration dword winapi  ThreadProc (Lpvoid param); Int&nbsP;main () { wsadata wsadata;   if  ( wsastartup ( MAKEWORD ( 2, 2 ),  &wsadata)   { /* start  winsock*/  printf (  Fail to start  up winsock.\n  ");   return -1;  }   //  Destination IP address, which is the IP address to ping  char szDestIp[] =  "119.75.217.109";  // 127.0.0.1 //   Create original  socket sraw = ::socket (AF_INET,&NBSP;SOCK_RAW,&NBSP;IPPROTO_ICMP);  //   Set the Receive timeout  settimeout (sraw, 1000, true); //  set the destination address  SOCKADDR_IN dest;  dest.sin_family = af_inet; dest.sin_port = htons (0);  dest.sin_addr. S_un. S_ADDR&NBSP;=&NBSP;INET_ADDR (Szdestip); //  Creating an ICMP packet  char buff[sizeof (ICMP_HDR)  +  1000]; ICMP_HDR* pIcmp =  (icmp_hdr*) buff; //  filling in ICMP packet data  pIcmp-> icmp_type = 8; // Request an ICMP echo  pIcmp->icmp_code = 0; pIcmp->icmp_id =  (USHORT):: GetCurrentProcessId (); picmp->icmp_checksum = 0; picmp->icmp_sequence = 0;  //  Fill Data section, can send 1KB byte data for any  memset (&buff[sizeof (ICMP_HDR)],  ' E ',  1000);   //  start sending and receiving ICMP packets  ushort nseq = 0; /////////measurement time send time is 15s/////////////  Clock_t start=clock ();  ////////Create thread Accept//////// handle handle=createthread (NULL,NULL, ThreadProc, (LPVOID) &sraw,0,null);  ::closehandle (handle);  ///////////////////////////////////////  static int ncount = 0; while (TRUE)  {   int nret;  //if (ncount++ == 4)   if (Clock ()-start>15000)//15 seconds after exiting    break;  picmp->icmp_checksum = 0;  picmp->icmp_timestamp  = ::gettickcount ();   picmp->icmp_sequence = nseq++;  //send 1KB bytes of data   picmp->icmp_checksum =  checksum ((ushort*) buff, sizeof (ICMP_HDR)  + 1000);   nret = ::sendto ( Sraw, buff, sizeof (ICMP_HDR)  + 1000, 0,  (sockaddr *) &dest, sizeof ( dest)   if (nret == socket_error)   {   printf (" sendto ()  failed: %d \n ",  ::wsagetlasterror ());   return -1;  }   ncount++;  //socket if it is not possible to send and receive data messages at the same time, pause sending, meaning   //sleep (+);  } sleep (20000 );  //test Result: The system sends 11MB data traffic  printf ("%d\n", ncount) within 15S;     ::wsacleanup ();  return 0;} Dword winapi threadproc (Lpvoid param) { clock_t start=clock (); int nRet;  int ncount=0;  //socketparam param=* ((socketparam*) pparam);  SOCKET sock=* ( (socket*) PA sockaddr_in from; int nlen=sizeof (from),  //allows the packet to arrive at the server side smoothly, then accepts the server-side message  : : Sleep (+),  while (Clock ()-start<10000)  {  char recvbuf[1400];  nret  = ::recvfrom (sock, recvbuf, 1400, 0,  (sockaddr*) &from, &nLen);   if (Nret == socket_error)   {   if (:: WSAGetLastError ()  ==  wsaetimedout)    {    printf (" timed out\n");     continue;   }   printf (" recvfrom ()  failed: %d\n",  ::wsagetlasterror ());   return -1;  }  //  start parsing the received ICMP packets below   int ntick = ::gettickcount ();   if (nret < sizeof (IPHeader)  + sizeof (ICMP_HDR))   {   printf (" too few bytes from  %s \n ",  ::inet_nToa (FROM.SIN_ADDR));  }  //  the received data contains the IP header, the IP header size is 20 bytes, so add 20 to get the ICMP header   icmp_hdr * precvicmp =  (icmp_hdr*) (recvbuf + 20); //  (ICMP_HDR*) (recvBuf +  sizeof (Ipheader))   if (precvicmp->icmp_type != 0)  //  echo   {    printf (" nonecho type %d recvd \n",  precvicmp->icmp_type);    return -1;  }  if (precvicmp->icmp_id != :: GetCurrentProcessId ())   {   printf (" someone else ' s packet! \n");    return -1;  }  ncount++; //compute the network speed, the packet size is 1k //calculation formula: ncount (number)/10 (s);  //ncount=ncount/10; //printf ("%d\n", ncount);   printf (" %d bytes  from %s: ",  nret, inet_ntoa (FROM.SIN_ADDR));   printf ("  icmp_seq =  %d.  ",  precvicmp->icmp_sequence);   printf (" time: %d ms",  ntick - precvicmp->icmp_ timestamp);   printf (" \n");   ::sleep (+);  }  return 0;}

Flow Speed Meter

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.