Page 1 of 1

Ping icmp UDP

Posted: Wed Jan 11, 2006 6:47 am
by Mij
Hello fellows,

i am doing some research about te possibilities of using DOS commands in the PureBasic programming.

fore example i want to send a ping with dos or pure basic and log the result in a file with pure basic.

some one don this before.

couldn't find any matching subjects !!

Thank you for responding.


related is the questing ,, how do i select to use UDP in stead of TCP in the network programming.

Again thank you for responding.
:arrow:

Posted: Wed Jan 11, 2006 8:00 am
by Tranquil
There are some sources in this forum. A search for 'Ping' gives the following thread as result:

viewtopic.php?t=3717&highlight=ping

For sockets relating to UDP I found the following link in this forum using the search function. It works fine for me.

viewtopic.php?t=4895&highlight=udp


Hope it helps.

Posted: Wed Jan 11, 2006 9:16 am
by Droopy
Code from Marc tweaked for Library purpose :

Code: Select all

;/ Author : Marc
; Default Message : "Ping from PureBasic"
; Default TimeOut : 500 ms

ProcedureDLL IPNum(IPAdress.s) ; Return a numerical IP Adress from a IPString
  IpAddress.l=MakeIPAddress(Val(StringField(IPAdress,1,".")),Val(StringField(IPAdress,2,".")),Val(StringField(IPAdress,3,".")),Val(StringField(IPAdress,4,"."))) 
  ProcedureReturn IpAddress
EndProcedure

ProcedureDLL.s HostnameToIP(ConputerName.s) ; Return as a String
  If Len(ConputerName) > 0 
    ResultIP.s=""    
    high.b = 1: low.b = 1 
    DefType.w wsaversion 
    PokeB(@wsaversion, high) 
    PokeB(@wsaversion + 1, low) 
    If WSAStartup_(wsaversion, wsa.WSAData) = #NOERROR ; Try to access Windows sockets stuff... 
      *host.HOSTENT = gethostbyname_(ConputerName)       ; Get host information for named computer... 
      If *host <> #Null      
        While PeekL(*host\h_list + AdressNumber * 4) 
          IpAddress = PeekL(*host\h_list + AdressNumber * 4) 
          ResultIP = StrU(PeekB(IpAddress),0)+"."+StrU(PeekB(IpAddress+1),0)+"."+StrU(PeekB(IpAddress+2),0)+"."+StrU(PeekB(IpAddress+3),0) 
          AdressNumber + 1 
        Wend 
      EndIf 
      WSACleanup_() ; Close Windows sockets stuff... 
    EndIf 
    ProcedureReturn ResultIP 
  EndIf 
EndProcedure 

ProcedureDLL Ping3(sIPAdress.s,TimeOut,Message.s)
  ;/ Renvoie le temps en ms
  ;/ Renvoie -1 si hôte inaccessible
  ;/ Renvoie -2 si la résolution du nom de l'hôte en adresse Ip a échouée
  
  Shared PingTTL
  
  ResultSize.l = SizeOf(ICMP_ECHO_REPLY) + Len(Message) 
  *Result = AllocateMemory(ResultSize) 
  *Echo.ICMP_ECHO_REPLY = *Result 
   
  If Len(sIPAdress ) > 0 
    hFile.l = IcmpCreateFile_() 
    IpAddress.l=MakeIPAddress(Val(StringField(sIPAdress,1,".")),Val(StringField(sIPAdress,2,".")),Val(StringField(sIPAdress,3,".")),Val(StringField(sIPAdress,4,".")))  
    If IPAdresse = 0 
      sIPAdress = HostnameToIP(sIPAdress) 
      IpAddress.l=MakeIPAddress(Val(StringField(sIPAdress,1,".")),Val(StringField(sIPAdress,2,".")),Val(StringField(sIPAdress,3,".")),Val(StringField(sIPAdress,4,"."))) 
    EndIf 
    If IpAddress > 0 
      
      If IcmpSendEcho_(hFile, IpAddress, Message, Len(Message), 0, *Result, ResultSize, TimeOut) > 0                                
        ; PrintN("Ping " + sIPAdress + " Octets: " + Str(*Echo\DataSize) + " Temps: " + Str(*Echo\RoundTripTime) + " ms TTL:" + StrU(*Echo\Options\Ttl,#Byte)) 
      Else 
        ;/ Hôte inaccessible 
        FreeMemory(*Result)
        ProcedureReturn -1
      EndIf 
      
      IcmpCloseHandle_(hFile) 
    Else 
      ;/ Nom d'hôte introuvable / inrésolvable
      FreeMemory(*Result) 
      ProcedureReturn -2
    EndIf 
  EndIf 
  FreeMemory(*Result)
  
  ;/ Définition variables partagées
  PingTTL=*Echo\Options\Ttl & $000000FF ;/ Car résultat sur un octet
   
  ProcedureReturn *Echo\RoundTripTime
EndProcedure

ProcedureDLL Ping2(sIPAdress.s,TimeOut)
  ProcedureReturn Ping3(sIPAdress.s,TimeOut,"Ping from PureBasic")
EndProcedure

ProcedureDLL Ping(sIPAdress.s)
  ProcedureReturn Ping3(sIPAdress.s,500,"Ping from PureBasic")
EndProcedure

ProcedureDLL PingGetTTL() ; Renvoie le TTL du poste pingué préalablement avec Ping
  Shared PingTTL
  ProcedureReturn PingTTL
EndProcedure


;/ Test Ping()
; #Host="www.voila.fr"
; Message.s+#Host+#CR$
; Message.s+Str(Ping(#Host))+" ms"+#CR$
; Message+Str(PingGetTTL())+" TTL"
; MessageRequester("Ping",Message)

;/ Test IPNum()
; ip= IPNum("192.168.0.1")
; Debug IPString(ip)

;/ Test HostNameToIP
; Debug HostnameToIP("www.voila.fr")

Posted: Sun Jan 22, 2006 12:11 am
by Mij
hi Tranquil
Enthusiast

Droopy


A bit late may be, but thanks for the tips, i had a lot of fun and a working programm finaly .


Thank you

Posted: Wed Mar 01, 2006 12:47 pm
by Michael Vogel
Found an error!

Pinging to adressed with the last Byte>127 will not work, because of a little coding error in the Procedure Ping3:

Code: Select all

If IpAddress > 0 
will not be true, because the created long variable gets negative - so just add a simple '<' to let the code work...

Michael

trick i use

Posted: Wed Aug 13, 2008 10:59 am
by alokdube
works for say a small NMS kinds, a quick code

Code: Select all

Compiler = RunProgram("ping.exe", "sun.com -n 2", "", #PB_Program_Open|#PB_Program_Read|#PB_Program_Hide )
  Output$ = ""
  If Compiler  
    While ProgramRunning(Compiler)
      Output$ + ReadProgramString(Compiler) + Chr(13)
    Wend
    Output$ + Chr(13) + Chr(13)
    Output$ + "Exitcode: " + Str(ProgramExitCode(Compiler))     
  EndIf
  MessageRequester("Output", Output$)

  Output$ = ""
  ;OpenConsole()
  If Compiler  
    While ProgramRunning(Compiler)
      Output$ + ReadProgramString(Compiler) + Chr(13)
    Wend
    Output$ + Chr(13) + Chr(13)
    Output$ + "Exitcode: " + Str(ProgramExitCode(Compiler))     
  EndIf

Posted: Wed Aug 13, 2008 7:30 pm
by superadnim
Do you know how to send broadcasts to class C and listen for them as well?.

Posted: Tue Aug 19, 2008 6:52 am
by alokdube
use a raw socket in linux, have done that, however, i dont think there is any application other than destructive using the same :)
for nms kind of apps the above code is good enuf

Posted: Tue Aug 19, 2008 1:04 pm
by superadnim
But I'm in Windows :o
Destructive?, no... I want to detect the other peer's IP. Both PCs have dynamic IPs, how can they communicate to each other unless I'm able to broadcast a "here I am" message to all addresses?.

Posted: Tue Aug 19, 2008 4:03 pm
by SFSxOI
Isn't that what ARP and multicast is for?

Posted: Wed Aug 20, 2008 10:18 am
by dell_jockey
superadnim wrote:Do you know how to send broadcasts to class C and listen for them as well?.
For class-c segments, x.x.x.0 is the segment designator, and the valid address range of individual devices is x.x.x.1 - x.x.x.254.
If you want to address all devices within a class-c segment at once, send the message to x.x.x.255.

Mind you, this is only valid for a straight class-c segment (subnet mask x.x.x.0) so it will not work on sub- or supernetted segments.

WARNING: addressing a x.x.x.255/24 address may cause unexpected results if - in a multi-segmented network - the routers are not configured properly. Normally, messages addressed to the broadcast address shouldn't be routed into other segments and properly configured routers don't. If you run your software on such a network, check out the router configuration first.... You don't want to cause a broadcast storm across an enterprise network...

As to listening for broadcasts: these are normal UPD/IP packets like any other, so why not set up a listener on the port you're expecting these broadcasts to arrive on?

Posted: Wed Aug 20, 2008 10:21 am
by dell_jockey
// inadvertent double post deleted