
Question:
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?
Answer1:I have not tried raw socket but your <em>Access Denied</em> likely means they are restricted in iOS - just like they <a href="http://www.unix.com/unix-advanced-expert-users/108252-why-root-permissions-required-creating-raw-socket.html" rel="nofollow">require</a> <strong>root</strong> privilege on UNIX system. This is the same reason why, in some cases, it was not possible to <em>directly</em> (workaround exists) <a href="http://www.mono-project.com/Gendarme.Rules.Portability#FeatureRequiresRootPrivilegeOnUnixRule" rel="nofollow">allow</a> the Ping
class to work on Mono for Linux systems.
The lack of Ping
is a <a href="http://bugzilla.xamarin.com/show_bug.cgi?id=964" rel="nofollow">known</a> issue and can <em>generally</em> be worked around by using the <a href="https://github.com/xamarin/monotouch-samples/blob/master/ReachabilitySample/reachability.cs" rel="nofollow">Reachability</a> helper. <a href="https://github.com/xamarin/monotouch-samples/tree/master/ReachabilitySample" rel="nofollow">Sample</a> code is available on GIT.
You can use the Xamarin.iOS/MonoTouch bindings of SimplePing (which Apple says works on iOS).
<a href="https://github.com/theonlylawislove/MonoTouch.SimplePing" rel="nofollow">https://github.com/theonlylawislove/MonoTouch.SimplePing</a>
Check the test project for demo usage. Works on simulator and device.