I'm trying to implement ping function in MonoTouch.
for some reasons, Ping
class from System.Net.NetworkInformation
doesn't work on the real device, so I decided to switch to raw sockets and implement ping myself.
Unfortunately, I'm getting "Access Denied" message on:
socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp);
Any idea how could ping be done in monotouch? And why can't I create raw sockets?
I have not tried raw socket but your Access Denied likely means they are restricted in iOS - just like they require root privilege on UNIX system. This is the same reason why, in some cases, it was not possible to directly (workaround exists) allow the Ping
class to work on Mono for Linux systems.
The lack of Ping
is a known issue and can generally be worked around by using the Reachability helper. Sample code is available on GIT.
You can use the Xamarin.iOS/MonoTouch bindings of SimplePing (which Apple says works on iOS).
https://github.com/theonlylawislove/MonoTouch.SimplePing
Check the test project for demo usage. Works on simulator and device.