System.Net.Sockets Connect function

Ayman Khuzundar 1 Reputation point
2021-06-02T14:08:42.733+00:00

I am trying to build my application using .net C# that works as a client to reach a certain device.
When trying my application in Android then it works very well but when turns into ios then the System.Net.Sockets Function "Connect" throws an exception NoRouteToHost !
here is how I write the code:

        public string IP = "192.168.1.80";
        public Int32 Port = 5000;
        byte[] SendBuf;
        byte[] RecBuf;
        int i;
        TcpClient TcpClient1;
        NetworkStream serverStream;
        Stopwatch Time = new Stopwatch();

  public Int16 MyFunction(UInt16 A, UInt16 n, UInt16[] W)
        {
            Int16 Result;
            TcpClient1 = new TcpClient(AddressFamily.InterNetwork);
            TcpClient1.Connect(IP, Port);
            SendBuf = new byte[12];

           // here I complete to write my code

            TcpClient1.Close();
            serverStream.Close();
            Time.Stop();

            return Result;
        }

then here is what I get in Xcode debug log:

SocketException: No route to host
  at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP) [0x00000] in <00000000000000000000000000000000>:0 
  at System.Net.Sockets.TcpClient.Connect (System.Net.IPEndPoint remoteEP) [0x00000] in <00000000000000000000000000000000>:0 
  at System.Net.Sockets.TcpClient.Connect (System.String hostname, System.Int32 port) [0x00000] in <00000000000000000000000000000000>:0 
  at EELE5452.Modbus.ReadRegisters (System.UInt16 A, System.UInt16 n, System.UInt16[] W) [0x00000] in <00000000000000000000000000000000>:0 
  at System.Action.Invoke () [0x00000] in <00000000000000000000000000000000>:0 
  at System.Threading.Tasks.Task.Execute () [0x00000] in <00000000000000000000000000000000>:0 
  at System.Threading.ContextCallback.Invoke (System.Object state) [0x00000] in <00000000000000000000000000000000>:0 
  at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00000] in <00000000000000000000000000000000>:0 
  at System.Threading.Tasks.Task.ExecuteWithThreadLocal (System.Threading.Tasks.Task& currentTaskSlot) [0x00000] in <00000000000000000000000000000000>:0 
  at System.Threading.Tasks.Task.ExecuteEntry (System.Boolean bPreventDoubleExecution) [0x00000] in <00000000000000000000000000000000>:0 
  at System.Threading.ThreadPoolWorkQueue.Dispatch () [0x00000] in <00000000000000000000000000000000>:0 
Rethrow as AggregateException: One or more errors occurred.
  at System.Threading.Tasks.Task.ThrowIfExceptional (System.Boolean includeTaskCanceledExceptions) [0x00000] in <00000000000000000000000000000000>:0 
  at System.Threading.Tasks.Task.Wait (System.Int32 millisecondsTimeout, System.Threading.CancellationToken cancellationToken) [0x00000] in <00000000000000000000000000000000>:0 
  at System.Threading.Tasks.Task.Wait (System.TimeSpan timeout) [0x00000] in <00000000000000000000000000000000>:0 
  at MainCode.SignIn () [0x00000] in <00000000000000000000000000000000>:0 
  at UnityEngine.Events.UnityAction.Invoke () [0x00000] in <00000000000000000000000000000000>:0 
  at UnityEngine.Events.UnityEvent.Invoke () [0x00000] in <00000000000000000000000000000000>:0 
  at UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1].Invoke (T1 handler, UnityEngine.EventSystems.BaseEventData eventData) [0x00000] in <00000000000000000000000000000000>:0 
  at UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) [0x00000] in <00000000000000000000000000000000>:0 
  at UnityEngine.EventSystems.StandaloneInputModule.ProcessTouchPress (UnityEngine.EventSystems.PointerEventData pointerEvent, System.Boolean pressed, System.Boolean released) [0x00000] in <00000000000000000000000000000000>:0 
  at UnityEngine.EventSystems.StandaloneInputModule.ProcessTouchEvents () [0x00000] in <00000000000000000000000000000000>:0 
  at UnityEngine.EventSystems.StandaloneInputModule.Process () [0x00000] in <00000000000000000000000000000000>:0 
UnityEngine.EventSystems.ExecuteEvents:Execute(GameObject, BaseEventData, EventFunction`1)
UnityEngine.EventSystems.StandaloneInputModule:ProcessTouchPress(PointerEventData, Boolean, Boolean)
UnityEngine.EventSystems.StandaloneInputModule:ProcessTouchEvents()
UnityEngine.EventSystems.StandaloneInputModule:Process()

Any Idea about how to solve this ? I've asked Apple's Fourm but haven't got any valuable answer.
Thanks

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,857 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.