Forum Xamarin.Android

MonoDroid socket access denied

I am porting an application from WM to Android using MonoDroid.

I am currently facing a problem where initializing a socket always throw an Exception 'SocketException: Access Denied' on this line:

using (Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp))

The source port is 9000 and destination port 9600.

The 'AndroidManifest.xml' file do have:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />

And 'AssemblyInfo.cs':

[assembly: UsesPermission(Android.Manifest.Permission.Internet)]
[assembly: UsesPermission(Android.Manifest.Permission.AccessNetworkState)]
[assembly: UsesPermission(Android.Manifest.Permission.AccessWifiState)]
[assembly: UsesPermission(Android.Manifest.Permission.ChangeNetworkState)]
[assembly: UsesPermission(Android.Manifest.Permission.ChangeWifiMulticastState)]
[assembly: UsesPermission(Android.Manifest.Permission.ChangeWifiState)]
[assembly: UsesPermission(Android.Manifest.Permission.WriteExternalStorage)]

Tried on two different devices(OS 2.3.5 and 4+), in both Debug and Release.

The problem is starting to drive me crazy.

Thanks in advance!


  • CheesebaronCheesebaron DKInsider, University mod

    I doubt that it is a problem with the permissions. It is not necessary to have them in both the AssemblyInfo.cs and AndroidManifest.xml files. Pick one.

  • YSharpYSharp USMember ✭✭
    edited January 2014

    Relevant, hopefully:

    "Raw sockets offer the capability to manipulate the underlying transport, so they can be used for malicious purposes that pose a security threat. Therefore, only members of the Administrators group can create sockets of type SOCK_RAW on Windows 2000 and later."

    Limitations on Raw Sockets

    "On Windows 7, Windows Vista, Windows XP with Service Pack 2 (SP2), and Windows XP with Service Pack 3 (SP3), the ability to send traffic over raw sockets has been restricted in several ways[...]"

    Of course, it's the corresponding Mono implementation that you might want to check out re: the limitations over raw sockets, ICMP, etc.

    For instance, see this SO thread:


  • Jean-PhilippeSavardJean-PhilippeSavard USMember
    edited January 2014

    Thanks for the reply.

    Actually, I am not exactly sure what i should do to fix this problem and make it works.

    The code do compile, i get the socketexception and i can't change the ports or socket type.

    Any suggestions?


    I tried adding:

    Java.Lang.Process p = Java.Lang.Runtime.GetRuntime().Exec("su");

    But i get an exception:

    Java.IO.IOException: Error running exec(). Command: [su] Working Directory: null Environment: null

  • It appear that we are using Raw Sockets for our Ping class only.

    So, i am now wondering if there is an equivalent to MonoTouch SimplePing( for MonoDroid? Would it work on it?

  • YSharpYSharp USMember ✭✭
    edited January 2014

    Check out the paragraph "ICMP ping throws an exception", in the section "Common problems", there:


  • Thank you for the reply and the help, YSharp.

    You considerably helped in reducing my headache, I'll consider my issue as fixed.

Sign In or Register to comment.