Tag: roundtrip

ICMP Ping Routine (Windows only)

Posted by on June 4, 2010

Heres my ping routine, It’s the weapon of choice for checking online status, update servers, or just plain pinging an IP for whatever reason happens to float your boat!

EnableExplicit
Procedure.i NetPing( IP_Address.s = "209.85.225.105", Timeout.i = 5000 ) ; Timeout in Milliseconds, Defaults to 5 seconds.
 
  ;Returns => 0 Upon pinging. Otherwise returns < 0 -- The actual return value is the roundtrip time in milliseconds.
 
  Define.i hIcmpFile, dwRetVal			= -1
  Define.s DataBuffer 				= "PING"
  Define.i ReplyBufferSize 			= SizeOf(ICMP_ECHO_REPLY) + Len(DataBuffer) + SizeOf(character)
  Define.ICMP_ECHO_REPLY *ReplyBuffer 		= AllocateMemory(ReplyBufferSize)
 
  If ( *ReplyBuffer )
 
	  hIcmpFile = IcmpCreateFile_()
 
	  If hIcmpFile
 
		 If IcmpSendEcho_( hIcmpFile, inet_addr_(IP_Address), @DataBuffer, Len(DataBuffer), #Null, *ReplyBuffer, ReplyBufferSize + SizeOf(ICMP_ECHO_REPLY), Timeout )
			dwRetVal = *ReplyBuffer\RoundTripTime
		 EndIf
 
		 IcmpCloseHandle_( hIcmpFile )
 
	  Else
	  	Debug "NetPing -- IcmpCreateFile returned false."
	  EndIf
 
	  FreeMemory( *ReplyBuffer )
 
  Else
  	Debug "NetPing -- Couldn't allocate memory."
  EndIf
 
  ProcedureReturn dwRetVal
 
EndProcedure

Enjoy!