Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



Share:
Notifications
Clear all

[Solved] Lazarus - MacOS X/Linux - Do a network Ping from within your appliccations

3 Posts
1 Users
0 Reactions
2,833 Views
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2724
Topic starter  

You could use Synapse to do a Ping, but so far I have had zero luck in executing any of the example codes - this appear related to required elevated admin rights to actually do a ping that way.
However ... you can do it by using the Terminal/Commandline "ping". 

You will need to include the "process" unit in the Uses Clause. 

Quickly wrapped in a simple function for MacOS X and Linux - This function WILL NOT WORK under Windows:
 

function TForm1.Ping(Host:string):boolean;
var s : ansistring;
const PingCount = '1'; // number of pings
      PingTimeout = '2'; // timout for each ping
begin
  {$IFDEF Darwin}
    RunCommand('/sbin/ping',['-c '+PingCount,'-t '+PingTimeout,'-q',Host],s);
  {$ELSE}
    RunCommand('/bin/ping',['-c '+PingCount,'-w '+PingTimeout,'-q',Host],s);
  {$ENDIF}
  Result := (pos('100.0% packet loss',S)=0) and (pos('100% packet loss',S)=0);
end; 

I've tested this under MacOSX and Linux (Ubuntu / Freya OS).

You will see that time out parameter under MacOS X (-t) is slightly different than under Linux (-w), and that the location of ping is different as well. 
The parameters and location might be different under different Linux versions as well.

   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2724
Topic starter  

A version that works for Windows (tested Windows 10) and MacOS X (tested with El Capitan) - downside for Windows is the briefly popping up of a shell window.

function Ping(Host:string):boolean;
var s : ansistring;
const PingCount = '2'; // number of pings
      PingTimeout = '2'; // timout for each ping
begin
  s:='';
  {$IFDEF Darwin}
  RunCommand('/sbin/ping',['-c '+PingCount,'-t '+PingTimeout,'-q',Host],s);
  {$ENDIF}
  {$IFDEF Windows}
  RunCommand('ping.exe',['-n ',PingCount,'-w ',PingTimeout,Host],s);
  {$ENDIF}
  Result := (pos('100.0% packet loss',S)=0) and (pos('100% packet loss',S)=0) and (pos('100% loss',S)=0);
end;

   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2724
Topic starter  

For those writing apps with the intend to place them in the Apple App Store:

Applications in the App Store are sandboxed, which means that without the proper entitlements, PING will not work.

After wasting an entire day on how to better implement ping (in code for example - note: Synapse's ping host function does not work under macOS), I found that you will need to set these 2 entitlements for ping to work (in the code I presented above):

com.apple.security.network.client
com.apple.security.network.server

One to send "signals" to the network, but the other one is also needed to receive the ping response. 

So you entitlements.plist file could look something like this:

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>com.apple.security.app-sandbox</key>
  <true/>
  <key>com.apple.security.network.server</key>
  <true/>
  <key>com.apple.security.network.client</key>
  <true/>
</dict>
</plist>

 

Hope that by posting this here I'm saving someone wasting a day as well 😉 

This post was modified 3 weeks ago by Hans

   
ReplyQuote
Share: