Raw sockets in monotouch

2019-06-07 06:00发布

问题:

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?

回答1:

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.



回答2:

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.