Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iOS only SocketException No route to host #14224

Closed
CsabaStupak opened this issue Mar 28, 2023 · 5 comments
Closed

iOS only SocketException No route to host #14224

CsabaStupak opened this issue Mar 28, 2023 · 5 comments
Labels
platform/iOS 🍎 s/needs-attention Issue has more information and needs another look t/bug Something isn't working

Comments

@CsabaStupak
Copy link

Description

I have a simple Maui application (.NET7 - created from template) which communicates with a local network server. I'm testing it using Android and iPhone. The code is simple:

TcpClient client = new TcpClient();
await client.ConnectAsync(IPAddress.Parse("192.168.3.10"), 12345);

or another approach:

Ping ping = new Ping();
await ping.SendPingAsync(IPAddress.Parse("192.168.3.10"));

In both case the result is SocketException (65): No route to host. This happens on iPhone only. On Android it works fine.

My Android phone IP is 192.168.3.45 and iPhone IP is 192.168.3.170. Both are on the same network as the server. First time I try to connect even the "Grant access to local network" appears on iPhone, what I allowed. I can see in the iPhone settings that my application has access to local network = on.

In the project I even added the following to the Info.plist:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsLocalNetworking</key>
    <true/>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

On my iPhone 13 mini, iOS 16.3.1 I can see the server using Fing - Network Scanner and its Device Discovery. I'm able to Ping it. So my phones are certainly on the same network. Since I can connect to the server via Android my server settings should be OK (firewall etc.). The given code works fine if I try to ping 8.8.8.8 address. So this is apparently a local network access issue - somehow iPhone ignores that setting.

Any idea what could be the problem? Thanks :-)

Steps to Reproduce

  1. Create a New .NET MAUI app or clone the following repository https://github.com/CsabaStupak/MauiLocNetIssue.git
  2. Add the above code in try/catch block to the OnCounterClicked method.
  3. Modify the Info.plist file.
  4. Connect to your local iPhone device and click the button and in the debugger see the exception thrown.

Link to public reproduction project repository

https://github.com/CsabaStupak/MauiLocNetIssue.git

Version with bug

7.0 (current)

Last version that worked well

Unknown/Other

Affected platforms

iOS

Affected platform versions

iOS 16.3.1

Did you find any workaround?

No workaround found and since our application is a relay between Bluetooth device and local network server so can't move forward :-(

Relevant log output

[0:] Ping to 192.168.3.10 from 192.168.3.170 failed: System.Net.NetworkInformation.PingException: An exception occurred during a Ping request.
 ---> System.Net.Sockets.SocketException (65): No route to host
   at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.CreateException(SocketError error, Boolean forAsyncThrow)
   at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.SendToAsync(Socket socket, CancellationToken cancellationToken)
   at System.Net.Sockets.Socket.SendToAsync(ReadOnlyMemory`1 buffer, SocketFlags socketFlags, EndPoint remoteEP, CancellationToken cancellationToken)
   at System.Net.NetworkInformation.Ping.SendIcmpEchoRequestOverRawSocketAsync(IPAddress address, Byte[] buffer, Int32 timeout, PingOptions options)
   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[<SendIcmpEchoRequestOverRawSocketAsync>d__70](<SendIcmpEchoRequestOverRawSocketAsync>d__70& stateMachine)
   at System.Net.NetworkInformation.Ping.SendIcmpEchoRequestOverRawSocketAsync(IPAddress address, Byte[] buffer, Int32 timeout, PingOptions options)
   at System.Net.NetworkInformation.Ping.SendPingAsyncCore(IPAddress address, Byte[] buffer, Int32 timeout, PingOptions options)
   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[<SendPingAsyncCore>d__84](<SendPingAsyncCore>d__84& stateMachine)
   at System.Net.NetworkInformation.Ping.SendPingAsyncCore(IPAddress address, Byte[] buffer, Int32 timeout, PingOptions options)
   at System.Net.NetworkInformation.Ping.SendPingAsyncInternal(IPAddress address, Int32 timeout, Byte[] buffer, PingOptions options)
   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[<SendPingAsyncInternal>d__51](<SendPingAsyncInternal>d__51& stateMachine)
   at System.Net.NetworkInformation.Ping.SendPingAsyncInternal(IPAddress address, Int32 timeout, Byte[] buffer, PingOptions options)
   at System.Net.NetworkInformation.Ping.SendPingAsync(IPAddress address, Int32 timeout, Byte[] buffer, PingOptions options)
   at System.Net.NetworkInformation.Ping.SendPingAsync(IPAddress address)
   at MauiApp7.MainPage.PingAsync(String ipAddr) in D:\Data\DF19\Garmin\Test\TestNet7\MauiApp7\MainPage.xaml.cs:line 44
   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[<PingAsync>d__3](<PingAsync>d__3& stateMachine)
   at MauiApp7.MainPage.PingAsync(String ipAddr)
   at MauiApp7.MainPage.OnCounterClicked(Object sender, EventArgs e) in D:\Data\DF19\Garmin\Test\TestNet7\MauiApp7\MainPage.xaml.cs:line 36
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.AsyncStateMachineBox`1[[System.Threading.Tasks.VoidTaskResult, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[MauiApp7.MainPage.<OnCounterClicked>d__2, MauiApp7, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].ExecutionContextCallback(Object s)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.AsyncStateMachineBox`1[[System.Threading.Tasks.VoidTaskResult, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[MauiApp7.MainPage.<OnCounterClicked>d__2, MauiApp7, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].MoveNext(Thread threadPoolThread)
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.AsyncStateMachineBox`1[[System.Threading.Tasks.VoidTaskResult, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[MauiApp7.MainPage.<OnCounterClicked>d__2, MauiApp7, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].MoveNext()
   at System.Threading.Tasks.AwaitTaskContinuation.<>c.<.cctor>b__17_0(Object state)
   at System.Threading.Tasks.AwaitTaskContinuation.RunCallback(ContextCallback callback, Object state, Task& currentTask)
   at System.Threading.Tasks.SynchronizationContextAwaitTaskContinuation.Run(Task task, Boolean canInlineContinuationTask)
   at System.Threading.Tasks.Task.RunContinuations(Object continuationObject)
   at System.Threading.Tasks.Task.FinishContinuations()
   at System.Threading.Tasks.Task`1[[System.Threading.Tasks.VoidTaskResult, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].TrySetResult(VoidTaskResult result)
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1[[System.Threading.Tasks.VoidTaskResult, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].SetExistingTaskResult(Task`1 task, VoidTaskResult result)
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.SetResult()
   at MauiApp7.MainPage.PingAsync(String ipAddr) in D:\Data\DF19\Garmin\Test\TestNet7\MauiApp7\MainPage.xaml.cs:line 53
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.AsyncStateMachineBox`1[[System.Threading.Tasks.VoidTaskResult, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Runtime.CompilerServices.IAsyncStateMachine, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].ExecutionContextCallback(Object s)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.AsyncStateMachineBox`1[[System.Threading.Tasks.VoidTaskResult, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Runtime.CompilerServices.IAsyncStateMachine, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext(Thread threadPoolThread)
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.AsyncStateMachineBox`1[[System.Threading.Tasks.VoidTaskResult, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Runtime.CompilerServices.IAsyncStateMachine, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext()
   at System.Threading.Tasks.SynchronizationContextAwaitTaskContinuation.<>c.<.cctor>b__8_0(Object state)
   at Foundation.NSAsyncSynchronizationContextDispatcher.Apply()
   at UIKit.UIApplication.xamarin_UIApplicationMain(Int32 , String[] , IntPtr , IntPtr , IntPtr& )
   at UIKit.UIApplication.UIApplicationMain(Int32 argc, String[] argv, IntPtr principalClassName, IntPtr delegateClassName)
   at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass)
   at MauiApp7.Program.Main(String[] args) in D:\Data\DF19\Garmin\Test\TestNet7\MauiApp7\Platforms\iOS\Program.cs:line 13
   at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo , Object , Span`1& , Exception& )
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
   at Xamarin.iOS.HotRestart.Application.Run(String[] args, Action`1 loadFrameworkAction, Action killApplicationAction)
   at Xamarin.PreBuilt.iOS.Applications.Main(String[] args) in /Users/builder/azdo/_work/1/s/xamarin-macios/msbuild/Xamarin.HotRestart.PreBuilt/Xamarin.PreBuilt.iOS/Main.cs:line 12
--- End of stack trace from previous location ---
   at System.Net.NetworkInformation.Ping.SendIcmpEchoRequestOverRawSocketAsync(IPAddress address, Byte[] buffer, Int32 timeout, PingOptions options)
   at System.Net.NetworkInformation.Ping.SendPingAsyncCore(IPAddress address, Byte[] buffer, Int32 timeout, PingOptions options)
   at System.Net.NetworkInformation.Ping.SendPingAsyncInternal(IPAddress address, Int32 timeout, Byte[] buffer, PingOptions options)
   --- End of inner exception stack trace ---
   at System.Net.NetworkInformation.Ping.SendPingAsyncInternal(IPAddress address, Int32 timeout, Byte[] buffer, PingOptions options)
   at MauiApp7.MainPage.PingAsync(String ipAddr) in D:\Data\DF19\Garmin\Test\TestNet7\MauiApp7\MainPage.xaml.cs:line 44
@CsabaStupak CsabaStupak added the t/bug Something isn't working label Mar 28, 2023
@jfversluis
Copy link
Member

If you create a iOS app without .NET MAUI (do dotnet new ios), does it work then? I kind of assume it doesn't. I don't think this is specific to .NET MAUI.

@jfversluis jfversluis added the s/needs-info Issue needs more info from the author label Mar 28, 2023
@ghost
Copy link

ghost commented Mar 28, 2023

Hi @CsabaStupak. We have added the "s/needs-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

@CsabaStupak
Copy link
Author

@jfversluis I'm a Windows developer I can't try the IOS only project. I own only an iPhone. The project seems to run only on Mac so don't know how to verify if this is the same case.

@ghost ghost added s/needs-attention Issue has more information and needs another look and removed s/needs-info Issue needs more info from the author labels Mar 28, 2023
@jfversluis
Copy link
Member

Alright I tried it for you. Both with an iOS project without .NET MAUI and with a .NET MAUI project, running on an iOS Simulator and a physical device I can't reproduce this issue. Then tried your specific reproduction and also that one runs fine.

image

I only modified the IP address to be one on my local network. The exception you see is to be expected since I don't have anything running on TCP port 4356.

So a couple of options come to mind:

  • Something isn't right from inside your network
  • You're hitting a limitation when using Hot Reload (the name we have for developing for iOS devices directly from Windows)

I'm leaning towards the first one since it does throw an exception that has a relevant message: no route to host. This indicates to me that the ping/connection operation is actually attempted, but fails. In both cases I don't think there is much we can do here and it will be pretty hard to diagnose on our end.

Closing this for now as this doesn't seem like a bug with us from what I can tell. Good luck!

@jfversluis jfversluis closed this as not planned Won't fix, can't repro, duplicate, stale Mar 28, 2023
@CsabaStupak
Copy link
Author

CsabaStupak commented Mar 28, 2023

@jfversluis Thank you very much for the fast response, really appreciate 👍

Now I don't know how to continue with this, since it works at your end. When I checked both iPhone and Android Wifi settings and also my server all are on the same network Mask = 255.255.255.0, gateway/router = 192.168.3.1. While Android does not have problem to ping the server the iPhone has. I found another Github issue #12731 maybe similar to mine where it looks like it is related to iOS version - I'm on 16.3.1. Can you tell me what version of iOS did you try the code? Maybe that is the culprit...

I also tried to disable Hot Reload but it did not help. I'm a network administrator as well, so I have some knowledge to network and I do not see any issues here at least from network part. Of course maybe I do not see something what is obvious... But I have almost no experience with iPhone. Maybe you could give me some advice, what else should I check.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
platform/iOS 🍎 s/needs-attention Issue has more information and needs another look t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants