Ping icmp UDP

Just starting out? Need help? Post your questions and find answers here.
Mij
New User
New User
Posts: 6
Joined: Tue Jan 10, 2006 9:02 pm
Location: Holland

Ping icmp UDP

Post 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:
Tranquil
Addict
Addict
Posts: 952
Joined: Mon Apr 28, 2003 2:22 pm
Location: Europe

Post 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.
Tranquil
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Post 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")
Mij
New User
New User
Posts: 6
Joined: Tue Jan 10, 2006 9:02 pm
Location: Holland

Post 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
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Post 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
alokdube
Enthusiast
Enthusiast
Posts: 148
Joined: Fri Nov 02, 2007 10:55 am
Location: India
Contact:

trick i use

Post 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
superadnim
Enthusiast
Enthusiast
Posts: 480
Joined: Thu Jul 27, 2006 4:06 am

Post by superadnim »

Do you know how to send broadcasts to class C and listen for them as well?.

:lol: should I bash the keyboard and give up?
:?
alokdube
Enthusiast
Enthusiast
Posts: 148
Joined: Fri Nov 02, 2007 10:55 am
Location: India
Contact:

Post 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
superadnim
Enthusiast
Enthusiast
Posts: 480
Joined: Thu Jul 27, 2006 4:06 am

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

:lol: should I bash the keyboard and give up?
:?
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

Isn't that what ARP and multicast is for?
dell_jockey
Enthusiast
Enthusiast
Posts: 767
Joined: Sat Jan 24, 2004 6:56 pm

Post 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?
Last edited by dell_jockey on Wed Aug 20, 2008 10:50 am, edited 5 times in total.
cheers,
dell_jockey
________
http://blog.forex-trading-ideas.com
dell_jockey
Enthusiast
Enthusiast
Posts: 767
Joined: Sat Jan 24, 2004 6:56 pm

Post by dell_jockey »

// inadvertent double post deleted
cheers,
dell_jockey
________
http://blog.forex-trading-ideas.com
Post Reply