SimplePing demonstrates ping (ICMP) send and receive.
A Xamarin.iOS and Xamarin.Mac binding for the Apple sample: https://developer.apple.com/library/content/samplecode/SimplePing
On most platforms ping requires privileges (it's implemented with a raw IP socket). Apple platforms include a special facility that allows you to ping without privileges. Specifically, you can open a special, non-privileged ICMP socket that allows you to send and receive pings.
To use the SimplePing class:
- create an instance of the
SimplePing
class and keep a reference to that instance - set the
Delegate
property or theStarted
event. - call
Start()
. - if things go well, your delegate's
DidStartWithAddress()
method, or theStarted
event, will be called; to send a ping, callSendPing()
- when
SimplePing
receives an ICMP packet, it will call theDidReceivePingResponsePacket()
method, or theResponseRecieved
event
SimplePing can be used from any thread but the use of any single instance must be confined to a specific thread. Moreover, that thread must run its run loop. In most cases it's best to use SimplePing from the main thread.
var pinger = new SimplePing("www.apple.com");
pinger.Started += (sender, e) => {
var endpoint = e.EndPoint;
pinger.SendPing(null);
};
pinger.ResponseRecieved += (sender, e) => {
var seq = e.SequenceNumber;
var packet = e.Packet;
};
pinger.Start();