Ping for OSX and Win

Does anyone know of a good way to do a Ping on OSX and Windows?

I’d prefer to keep it consistent on both platforms and thus not use a shell to different command line tools.

I recently started using DarwinPingMBS in a project and discovered it apparently no longer works on Windows. (.Ping always returns -1 on Windows) Searching the release notes I find it’s also been deprecated. The MBS FAQ has some alternate recommendations, but none of them seem consistent cross platform.

I’d even settle for a way to read and write IP Datagrams and code up the Ping myself. Ideas?

Shell to it.

Not sure If you undertand your problem. You want to run ping from XOJO with shell command?
Then it could be helpful to know, that on windows any output from console can be redirected to a textfile

ping anyhost >textfile.txt

Parsing and analyzing the textfile should be easy then.

EDIT: You could use %TEMP% as placeholder to write the textfile in Standard temp folder. Otherwise it could collide with policy or wouldn’t work on media without writing priviledges.

It is exactly the same syntax on both Mac and Windows, and Tomas tip works just the same.

However, on Mac you got to terminate the shell to stop the pinging.

At one point when the system provides such a reliable tool built-in, why bother ?

Check this one:

ping -c 4 hostname to ping hostname four times.

You can ping without the command line using the ICMP Socket.

Documentation here.

This indeed uses the shell, but it is cross platform between OS X and Windows.

[code]Private Function Ping(HostName as string, optional TimeOut as Integer = 3600) As Double
'PING by Christian Wheel

’ Inputs: Hostname, TimeOut (optional, defaults to 3600 ms)

’ Returns:

’ -1: Could not resolve hostname
’ 0: Ping timed out
’ >0: Ping time in ms

’ Note that OS X may return a decimal, while Windows will always return a whole integer.

’ Max timeout on OS X is 3600 ms.

dim s as new shell, result as string
s.Mode=0 //Synchronous mode
#If TargetWin32 then
s.execute "ping -n 1 -w “+TimeOut.ToText+” " + Hostname
if instr(Result, “could not find host”) > 0 then return -1
if instr(Result, “100% loss”) > 0 then return 0
Result=mid(Result, instr(Result, “time=”)+5)
Return Val(Result)
#Elseif TargetMacOS then
s.execute "ping -c 1 -t " + TimeOut.ToText+ " "+ Hostname
if instr(Result, “cannot resolve”) > 0 then return -1
if instr(Result, “100.0% packet loss”) > 0 then return 0
Result=mid(Result, instr(Result, “time=”)+5)
Return Val(Result)
End Function