[windows] PING

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

[windows] PING

Post by BackupUser »

Code updated for 5.20+

Restored from previous forum. Originally posted by Rings.

I translated this small routine to PureBasic.Some of you find it usefull before trying and using some other network related networkfunctions.
A Result of 0 and below 20ms is okay,negatives are faults.

Code: Select all

;
;
;Ping (c) 2002 by Siegfried Rings (known as the 'CodeGuru' )
;
;
Procedure lngNewAddress(strAdd.s)
sDummy.s=strAdd
Position = FindString(sDummy, ".",1) 
If Position>0 
 A1=Val(Left(sDummy,Position-1))
 sDummy=Right(Sdummy,Len(Sdummy)-Position)
 Position = FindString(sDummy, ".",1) 
 If Position>0 
  A2=Val(Left(sDummy,Position-1))
  sDummy=Right(Sdummy,Len(Sdummy)-Position)
  Position = FindString(sDummy, ".",1) 
  If Position>0 
   A3=Val(Left(sDummy,Position-1))
   sDummy=Right(Sdummy,Len(Sdummy)-Position)
   A4=Val(sDummy)
   Dummy.l=0
   PokeA(@Dummy,A1)
   PokeA(@Dummy+1,A2)
   PokeA(@Dummy+2,A3)
   PokeA(@Dummy+3,A4)
   ProcedureReturn Dummy
  EndIf
 EndIf  
EndIf
EndProcedure


Procedure Ping(strAdd.s)

  #PING_TIMEOUT = 200

  lngHPort.l
  lngDAddress.l
  strMessage.s
  lngResult.l
  ECHO.ICMP_ECHO_REPLY

  strMessage = "Echo This."
  lngDAddress = lngNewAddress(strAdd)
  lngHPort = IcmpCreateFile_()
  lngResult = IcmpSendEcho_(lngHPort, lngDAddress, @strMessage, Len(strMessage), 0,@ECHO,SizeOf(ICMP_ECHO_REPLY), #PING_TIMEOUT)
  If lngResult = 0 
    PingResult = ECHO\Status * -1
  Else
    PingResult = ECHO\RoundTripTime
  EndIf
  lngResult = IcmpCloseHandle_(lngHPort)
  ProcedureReturn PingResult
EndProcedure


MessageRequester("Info",Str(Ping("127.0.0.1")),0)
 
Its a long way to the top if you wanna .....CodeGuru
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tranquil.

I only get negative values. I only get zero if I ping my localhost or my own IP. Anyone else?

Tranquilizer/ Secretly!
http://www.secretly.de
Registred PureBasic User
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Fangbeast.
Originally posted by tranquil

I only get negative values. I only get zero if I ping my localhost or my own IP. Anyone else?

Tranquilizer/ Secretly!
http://www.secretly.de
Registred PureBasic User
Correct me if I am wrong but pinging your 'localhost' or own IP is the equivalent of a TCPIP local loopback and therefore 0 would be correct?

Fangles
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fweil.

Hello,

0 means a good ping reply but with a short delay.

Only negative values should be errors (ie unreachable address or no network).

Positive or null value mean 'reply time' in milliseconds.

Rings , your snippet works anyway.

Francois Weil
14, rue Douer
F64100 Bayonne
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tranquil.

Thanks a lot. I only get negative values by trying to ping google.com on 216.239.55.100.

Cheers
Mike

Tranquilizer/ Secretly!
http://www.secretly.de
Registred PureBasic User
Post Reply