Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I avoid Access Denied error when calling TcpListener.Start() on iOS?

Tags:

c#

ios

sockets

mono

In my iOS application, I am trying to invoke a TcpListener instance. I define and start the listener as follows:

var listener = new TcpListener(IPAddress.Any, 104);
listener.Start();

However, I get the following exception when I run this code (iOS Simulator, iPhone, Debug mode):

System.Net.Sockets.SocketException: Access denied at System.Net.Sockets.Socket.Bind (System.Net.EndPoint local_end) [0x00051] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net.Sockets/Socket.cs:1111 at System.Net.Sockets.TcpListener.Start (Int32 backlog) [0x00022] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net.Sockets/TcpListener.cs:259 at System.Net.Sockets.TcpListener.Start () [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net.Sockets/TcpListener.cs:239 at
...

Is there anything I can do to avoid the Access denied error, or is this a built-in restriction of the Mono runtime on Xamarin.iOS?

like image 767
Anders Gustafsson Avatar asked Oct 29 '13 08:10

Anders Gustafsson


1 Answers

you can't bind to ports < 1024 without administrative privileges. Use a port >= 1024 for this purpose. This is a general restriction - you would encounter the same problem on any application if running as an ordinary user in most operating systems.

This is noted in the wikipedia page for DICOM, where it gives the list of ports:

DICOM have reserved the following TCP and UDP port numbers by the Internet Assigned Numbers Authority (IANA):

  • 104 well-known port for DICOM over Transmission Control Protocol (TCP) or User Datagram Protocol (UDP). Since 104 is in the reserved subset, many operating systems require special privileges to use it.
  • 2761 registered port for DICOM using Integrated Secure Communication Layer (ISCL) over TCP or UDP
  • 2762 registered port for DICOM using Transport Layer Security (TLS) over TCP or UDP
  • 11112 registered port for DICOM using standard, open communication over TCP or UDP

The standard recommends but does not require the use of these port numbers.

(page from wikipedia, emphasis mine)

like image 178
Petesh Avatar answered Nov 15 '22 05:11

Petesh