By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,086 Members | 1,460 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,086 IT Pros & Developers. It's quick & easy.

Slow response when executing Ping

P: 57
i did a program in vc++.net for ICMP communication using sockets.But when i execute it i received a very slow response...is there any configuration to be made on the server..or any other thing?.. i don't know the reason for slow response...i included the code for this...plz solve my problem...

Expand|Select|Wrap|Line Numbers
  1. // pingnew.h
  2.  
  3. #pragma once
  4. using namespace System;
  5. using namespace System::IO;
  6. using namespace System::Text; 
  7. using namespace System::Net;
  8. using namespace System::Net::Sockets;
  9. using namespace System::Web;
  10.  
  11. #define ICMP_ECHOREPLY 0
  12. #define ICMP_ECHOREQUEST 8
  13. #define MAX_TTL 256
  14. #define PACKET_SIZE 32
  15.  
  16. namespace pingnew
  17. {
  18.     public __gc class ICMP_Packet
  19.     {
  20.     public:
  21.         Byte Type;
  22.         Byte Code;
  23.         unsigned short Checksum;
  24.         unsigned short ID;
  25.         unsigned short Seq_no;
  26.         static Byte Data[]=new Byte[PACKET_SIZE];
  27.  
  28.     };
  29.     public __gc class ICMP_pro
  30.     {
  31.     public:
  32.      void SendPacket(String *s);
  33.      //static Byte CreatePack(ICMP_Packet *pac)[];
  34.     };
  35.     void ICMP_pro::SendPacket(String *s)
  36.     {
  37.         Socket *st=0;
  38.         IPHostEntry *iphe=0;
  39.         try
  40.         {
  41.         iphe = Dns::Resolve(s);
  42.         Console::WriteLine(S"Host Entry :{0}",iphe->HostName->ToString()); 
  43.         IPAddress* ipad = __try_cast<IPAddress*>(iphe->AddressList[0]);
  44.         Console::WriteLine(S"IP Address : {0}",ipad->ToString()); 
  45.         IPEndPoint* ipe = new IPEndPoint(ipad, 80);
  46.         Console::WriteLine(S"IP Address End Point: {0}",ipe->ToString()); 
  47.         st=new Socket(ipe->AddressFamily,SocketType::Raw,ProtocolType::Icmp);
  48.         //st->Bind(ipe);
  49.         ICMP_Packet *packet=new ICMP_Packet();
  50.         packet->Type=ICMP_ECHOREQUEST;
  51.         packet->Code=0;
  52.         packet->Checksum=0;
  53.         packet->ID=unsigned short(DateTime::Now.Millisecond);   
  54.         packet->Seq_no=0;
  55.         packet->Data=new Byte[PACKET_SIZE];
  56.         Console::WriteLine(S"Length :{0}",packet->Data->Length.ToString());    
  57.         for(int i=0;i<PACKET_SIZE;i++)
  58.         {
  59.             packet->Data[i]=(Byte)'S';
  60.             Console::WriteLine(S"Data :{0}",packet->Data[i].ToString());   
  61.         }
  62.  
  63.         //Byte BytetoSend[]=new Byte[];
  64.         /*ICMP_pro *pro=new ICMP_pro();
  65.         BytetoSend=pro->CreatePack(packet);*/
  66.  
  67.         /*to convert to byte*/
  68.         Byte packetArray[]=new Byte[PACKET_SIZE+8];
  69.         bool isLittleEndian=BitConverter::IsLittleEndian;
  70.  
  71.         UInt16 cksum=packet->Checksum;
  72.         for(int c=0;c<packetArray->Length;c+=2)
  73.             cksum+=Convert::ToInt32(BitConverter::ToUInt16(packetArray,c));  
  74.         cksum=(cksum>>16)+(cksum & 0xffff);
  75.         cksum+=(cksum >>16);
  76.         cksum=~cksum;
  77.         UInt16 id=packet->ID;
  78.         UInt16 seq=packet->Seq_no;
  79.         int index=0;
  80.         packetArray[index++]=packet->Type;
  81.         packetArray[index++]=packet->Code;
  82.         Byte temp[]=BitConverter::GetBytes(cksum);
  83.         Array::Copy(temp,0,packetArray,index,temp->Length);  
  84.         index +=2;
  85.         temp=BitConverter::GetBytes(id);
  86.         Array::Copy(temp,0,packetArray,index,temp->Length);
  87.         index +=2;
  88.         temp=BitConverter::GetBytes(seq);
  89.         Array::Copy(temp,0,packetArray,index,temp->Length);
  90.         index +=2;
  91.         Array::Copy(packet->Data,0,packetArray,index,PACKET_SIZE);  
  92.         for(Byte i=0;i<packetArray->Length;i++)
  93.         {
  94.             Console::Write(S" {0}",packetArray[i].ToString());
  95.         }
  96.         Console::WriteLine(S""); 
  97.         IPEndPoint* endPoint = new IPEndPoint(Dns::GetHostByName(Dns::GetHostName())->AddressList[0],80);
  98.         EndPoint* senderRemote = __try_cast<EndPoint*>(endPoint);
  99.         Console::WriteLine(S"SenderRemote :{0}",senderRemote->ToString()); 
  100.  
  101.         for(int j=0;j<=MAX_TTL;j++)
  102.           {
  103.             Byte RecvByte[]=new Byte[256];
  104.             st->SetSocketOption(SocketOptionLevel::IP,SocketOptionName::IpTimeToLive,j); 
  105.             st->SetSocketOption(SocketOptionLevel::Socket,SocketOptionName::SendTimeout,500000);
  106.             st->SetSocketOption(SocketOptionLevel::Socket,SocketOptionName::ReceiveTimeout,500000);
  107.             DateTime dt=DateTime::Now;
  108.             int iRet=st->SendTo(packetArray,packetArray->Length,SocketFlags::None,ipe);
  109.  
  110.             Console::WriteLine(S"Address :{0}",ipe->MaxPort.ToString());  
  111.             if(iRet==-1)
  112.             {
  113.                 Console::WriteLine(S"Error Sending Data"); 
  114.             }
  115.             else
  116.             {
  117.                 Console::WriteLine(S"Data Sending to {0}\n",ipe->ToString()); 
  118.             }
  119.             //Console::WriteLine(S"Address :{0}",*(&senderRemote)); 
  120.             iRet=st->ReceiveFrom(RecvByte,RecvByte->Length,SocketFlags::None,&senderRemote);
  121.             TimeSpan ts=DateTime::Now -dt;
  122.             if(iRet==-1)
  123.             {
  124.                     Console::WriteLine(S"Error Getting Data"); 
  125.             }
  126.             Console::WriteLine(S"Received Data :TTL={0,-5} IP={1,-20} Time={2,3}ms",j.ToString(),senderRemote->ToString(),ts.Milliseconds.ToString());   
  127.             if((iRet==PACKET_SIZE+ 8+ 20) && (BitConverter::ToInt16(RecvByte,24)==BitConverter::ToInt16(packetArray,4))&& (RecvByte[20]==ICMP_ECHOREPLY))
  128.                 break;
  129.             Console::WriteLine(S"20th Byte of Received Data :{0}",RecvByte[20].ToString());
  130.             for(Byte rb=0;rb<35;rb++)
  131.            {
  132.             Console::Write(S" {0}",RecvByte[rb].ToString());
  133.            } 
  134.            Console::WriteLine(S""); 
  135.             if(RecvByte[20]!=ICMP_ECHOREPLY)
  136.             {
  137.                 Console::WriteLine("UnExpected Reply,Quitting...");
  138.                 break;
  139.             }
  140.       }//for
  141.     }//try
  142.         catch(SocketException *e)
  143.         {
  144.             Console::WriteLine(S"Socket Exception : {0}",e->ToString());
  145.         }
  146.         catch(Exception *e)
  147.         {
  148.             Console::WriteLine(S"Exception :{0}",e->ToString());  
  149.         }
  150.   }//sendpacket
  151. };//namespace
  152.  
  153.  
and my main program is
Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. #using <mscorlib.dll>
  3. #include<conio.h>
  4. using namespace System;
  5. using namespace System::IO; 
  6. using namespace pingnew;
  7.  
  8. Int32 main(void)
  9. {
  10.     ICMP_pro *pro1=new ICMP_pro();
  11.     pro1->SendPacket("172.20.20.28"); 
  12.     int d=getch();
  13.     return 0;
  14. }
  15.  
Jun 16 '08 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.