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!

Posts

  • 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:

    http://msdn.microsoft.com/en-us/library/ms740548.aspx

    "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:

    http://stackoverflow.com/a/10254989

    'HTH,

  • 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?

    Edit:

    I tried adding:

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

    But i get an exception:
    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(https://github.com/theonlylawislove/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:

    http://www.mono-project.com/FAQ:_Technical#Common_Problems

    'HTH,

  • 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.