question

AymanKhuzundar-3144 avatar image
0 Votes"
AymanKhuzundar-3144 asked RobCaplan edited

System.Net.Sockets Connect function

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


dotnet-csharp
· 11
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

"No route to host" is a well known TCP error message indicating that your network setup is incorrect so no connection can be made to "192.168.1.80". Analyze your network settings or find a network expert in house to help you out. This is not something a forum can help much (no wonder Apple's forum didn't help).

0 Votes 0 ·

Thanks Iextm for your reply, unfortunately my problem isn't because of networks settings because I can reach the desired IP address using the same code but an android device or even my PC !
so, what do you think about this ? please I do need so much help in this problem that I've been trying to solve it since nearly 2 months :(

0 Votes 0 ·
lextm avatar image lextm AymanKhuzundar-3144 ·

Did you check if setting socket option IP_BOUND_IF helps? https://developer.apple.com/forums/thread/658518?answerId=631476022#631476022 For such socket related issues, you might try to write an equivalent iOS app in Swift or Objective C so as to test out the basic functionalities, and then port to C#.

1 Vote 1 ·
Show more comments

@AymanKhuzundar-3144 About "No route to host" issue, you can refer to the steps here to check it.

0 Votes 0 ·

Thanks for your reply,
Please if you can go with me step by step:
Now I made a debug flag to make sure where the Exception happens, The flag’s value is 7 -just before the tcpclient.connect(ip,port); function
Then is there a function to replace it with this one ? Or shall I be written the function in another way ?
Related to the post you shared above, The network works very well and no firewall is not active in my ios device

0 Votes 0 ·

@AymanKhuzundar-3144 Did you try to add some code to check if network is available in your app?

1 Vote 1 ·
Show more comments

0 Answers