Slow response when executing Ping

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • selvialagar
    New Member
    • Apr 2008
    • 57

    Slow response when executing Ping

    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...

    Code:
    // pingnew.h
    
    #pragma once
    using namespace System;
    using namespace System::IO;
    using namespace System::Text; 
    using namespace System::Net;
    using namespace System::Net::Sockets;
    using namespace System::Web;
    
    #define ICMP_ECHOREPLY 0
    #define ICMP_ECHOREQUEST 8
    #define MAX_TTL 256
    #define PACKET_SIZE 32
    
    namespace pingnew
    {
    	public __gc class ICMP_Packet
    	{
    	public:
            Byte Type;
    		Byte Code;
    		unsigned short Checksum;
    		unsigned short ID;
    		unsigned short Seq_no;
    		static Byte Data[]=new Byte[PACKET_SIZE];
    		
    	};
    	public __gc class ICMP_pro
    	{
    	public:
         void SendPacket(String *s);
    	 //static Byte CreatePack(ICMP_Packet *pac)[];
    	};
    	void ICMP_pro::SendPacket(String *s)
    	{
    		Socket *st=0;
    		IPHostEntry *iphe=0;
    		try
    		{
    		iphe = Dns::Resolve(s);
    		Console::WriteLine(S"Host Entry :{0}",iphe->HostName->ToString()); 
    		IPAddress* ipad = __try_cast<IPAddress*>(iphe->AddressList[0]);
    		Console::WriteLine(S"IP Address : {0}",ipad->ToString()); 
    		IPEndPoint* ipe = new IPEndPoint(ipad, 80);
            Console::WriteLine(S"IP Address End Point: {0}",ipe->ToString()); 
    		st=new Socket(ipe->AddressFamily,SocketType::Raw,ProtocolType::Icmp);
    		//st->Bind(ipe);
    		ICMP_Packet *packet=new ICMP_Packet();
    		packet->Type=ICMP_ECHOREQUEST;
    		packet->Code=0;
    		packet->Checksum=0;
    		packet->ID=unsigned short(DateTime::Now.Millisecond);   
    		packet->Seq_no=0;
    		packet->Data=new Byte[PACKET_SIZE];
    		Console::WriteLine(S"Length :{0}",packet->Data->Length.ToString());    
    		for(int i=0;i<PACKET_SIZE;i++)
    		{
    			packet->Data[i]=(Byte)'S';
    			Console::WriteLine(S"Data :{0}",packet->Data[i].ToString());   
    		}
    		
    	    //Byte BytetoSend[]=new Byte[];
            /*ICMP_pro *pro=new ICMP_pro();
    		BytetoSend=pro->CreatePack(packet);*/
    		
    		/*to convert to byte*/
            Byte packetArray[]=new Byte[PACKET_SIZE+8];
    		bool isLittleEndian=BitConverter::IsLittleEndian;
    
    		UInt16 cksum=packet->Checksum;
    		for(int c=0;c<packetArray->Length;c+=2)
    			cksum+=Convert::ToInt32(BitConverter::ToUInt16(packetArray,c));  
    		cksum=(cksum>>16)+(cksum & 0xffff);
    		cksum+=(cksum >>16);
            cksum=~cksum;
    		UInt16 id=packet->ID;
    		UInt16 seq=packet->Seq_no;
    		int index=0;
    		packetArray[index++]=packet->Type;
    		packetArray[index++]=packet->Code;
    		Byte temp[]=BitConverter::GetBytes(cksum);
    		Array::Copy(temp,0,packetArray,index,temp->Length);  
    		index +=2;
    		temp=BitConverter::GetBytes(id);
    		Array::Copy(temp,0,packetArray,index,temp->Length);
    		index +=2;
    		temp=BitConverter::GetBytes(seq);
            Array::Copy(temp,0,packetArray,index,temp->Length);
    		index +=2;
    		Array::Copy(packet->Data,0,packetArray,index,PACKET_SIZE);  
    		for(Byte i=0;i<packetArray->Length;i++)
    		{
    			Console::Write(S" {0}",packetArray[i].ToString());
    		}
    		Console::WriteLine(S""); 
    		IPEndPoint* endPoint = new IPEndPoint(Dns::GetHostByName(Dns::GetHostName())->AddressList[0],80);
    		EndPoint* senderRemote = __try_cast<EndPoint*>(endPoint);
    		Console::WriteLine(S"SenderRemote :{0}",senderRemote->ToString()); 
    			
    	    for(int j=0;j<=MAX_TTL;j++)
    		  {
    			Byte RecvByte[]=new Byte[256];
    			st->SetSocketOption(SocketOptionLevel::IP,SocketOptionName::IpTimeToLive,j); 
    			st->SetSocketOption(SocketOptionLevel::Socket,SocketOptionName::SendTimeout,500000);
    			st->SetSocketOption(SocketOptionLevel::Socket,SocketOptionName::ReceiveTimeout,500000);
    			DateTime dt=DateTime::Now;
    			int iRet=st->SendTo(packetArray,packetArray->Length,SocketFlags::None,ipe);
    			
    			Console::WriteLine(S"Address :{0}",ipe->MaxPort.ToString());  
    			if(iRet==-1)
    			{
    				Console::WriteLine(S"Error Sending Data"); 
    			}
    			else
    			{
    				Console::WriteLine(S"Data Sending to {0}\n",ipe->ToString()); 
    			}
    			//Console::WriteLine(S"Address :{0}",*(&senderRemote)); 
    			iRet=st->ReceiveFrom(RecvByte,RecvByte->Length,SocketFlags::None,&senderRemote);
    			TimeSpan ts=DateTime::Now -dt;
    			if(iRet==-1)
    			{
    					Console::WriteLine(S"Error Getting Data"); 
    			}
    			Console::WriteLine(S"Received Data :TTL={0,-5} IP={1,-20} Time={2,3}ms",j.ToString(),senderRemote->ToString(),ts.Milliseconds.ToString());   
    			if((iRet==PACKET_SIZE+ 8+ 20) && (BitConverter::ToInt16(RecvByte,24)==BitConverter::ToInt16(packetArray,4))&& (RecvByte[20]==ICMP_ECHOREPLY))
    				break;
    			Console::WriteLine(S"20th Byte of Received Data :{0}",RecvByte[20].ToString());
    			for(Byte rb=0;rb<35;rb++)
    		   {
    			Console::Write(S" {0}",RecvByte[rb].ToString());
    		   } 
    		   Console::WriteLine(S""); 
    			if(RecvByte[20]!=ICMP_ECHOREPLY)
    			{
    				Console::WriteLine("UnExpected Reply,Quitting...");
    				break;
    			}
          }//for
    	}//try
    		catch(SocketException *e)
    		{
    			Console::WriteLine(S"Socket Exception : {0}",e->ToString());
    		}
    		catch(Exception *e)
    		{
    			Console::WriteLine(S"Exception :{0}",e->ToString());  
    		}
      }//sendpacket
    };//namespace
    and my main program is
    Code:
    #include "stdafx.h"
    #using <mscorlib.dll>
    #include<conio.h>
    using namespace System;
    using namespace System::IO; 
    using namespace pingnew;
    
    Int32 main(void)
    {
        ICMP_pro *pro1=new ICMP_pro();
    	pro1->SendPacket("172.20.20.28"); 
    	int d=getch();
        return 0;
    }
Working...