Page 1 of 1

Posted: Tue Oct 08, 2002 11:59 am
by BackupUser
Restored from previous forum. Originally posted by Nessie.

Is there a command that will let me ping a remote server? I have tried the ping routine that I found elsewhere on the site, but find it difficult to understand. I basicaly only want to know if the remote server replies or not. Any help would be most appreciated !!!

Posted: Tue Oct 08, 2002 1:03 pm
by BackupUser
Restored from previous forum. Originally posted by tranquil.

Which server do you want to connect to?

Connect to it, if connection is successfull the server is still online. :)

;-)

Mike

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

Posted: Tue Oct 08, 2002 1:20 pm
by BackupUser
Restored from previous forum. Originally posted by Rings.

viewtopic.php?t=1106

its not so difficult as it seems.....

Its a long way to the top if you wanna .....CodeGuru

Posted: Tue Oct 08, 2002 2:56 pm
by BackupUser
Restored from previous forum. Originally posted by Nessie.

Thanks Rings, Tell me if I have this now.

reply=ping("IPAddress")

if reply =>0 And reply <=200 then etc......

It seems to work.

Ta.

:)

Posted: Tue Oct 08, 2002 6:29 pm
by BackupUser
Restored from previous forum. Originally posted by tranquil.

Do it so:

Code: Select all


Structure HOSTENT
      h_name.l
      h_aliases.l
      h_addrtype.w
      h_length.w
      h_addr_list.l
EndStructure


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
        PokeB(@Dummy,A1)
        PokeB(@Dummy+1,A2)
        PokeB(@Dummy+2,A3)
        PokeB(@Dummy+3,A4)
        ProcedureReturn Dummy
      EndIf
    EndIf 
  EndIf
  EndProcedure



Procedure ping(ip$)
  
  ; Get IP of Host
    
  pHostinfo = gethostbyname_(ip$)                             ; Get IP from Hostname
  
  If pHostinfo = 0                                            ; Successfull? 0 if not!
    ping=-1                                                   ; Return -1 couse resolve fails
  Else                                                        ; Everything is okay right now....
    CopyMemory (pHostinfo, hostinfo.HOSTENT, SizeOf(HOSTENT))  ; Copy the Datas to our Structure

    If hostinfo\h_addrtype  #AF_INET                         ; Successfull?
      ping=-1                                                  ; Not!
    Else   
      ipAddress = PeekL(hostinfo\h_addr_list)                  ; Everything allright here, get the pointer to our IP Address
      ip$=StrU(PeekB(ipAddress),0)+"."+StrU(PeekB(ipAddress+1),0)         ; Form our IP Address here
      ip$+"."+StrU(PeekB(ipAddress+2),0)+"."+StrU(PeekB(ipAddress+3),0)   ; and here
    EndIf
  EndIf

  If ping-1                   ; Now we have our IP and we can ping...
    strAdd.s=ip$                ; Our IP to ping for
    #PING_TIMEOUT = 20000        ; Max. wait for replay time (Ping time out)
    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
  EndIf
  
  EndProcedure

[/code)


after that you should use result=ping("IP")

Hope that is what you want, and hope it works. Couse I snipped this little part out of an older source. The Ping Routine is Based on Rings Source posted in this forum.

Cheers
Mike

Tranquilizer/ Secretly!
[url]http://www.secretly.de[/url]
Registred PureBasic User

Posted: Tue Oct 08, 2002 9:43 pm
by BackupUser
Restored from previous forum. Originally posted by Nessie.

Sorry Mike, I couldn't get the code to work. However I did manage to use Rings code so as I could see if the remote Ip replied and also set the timeout. Thanks to you both.