Xamarin.Forms - TcpClient connection fails with System.Net.Sockets.SocketException (0x80004005): Network is unreachable

不羁岁月 提交于 2020-01-03 06:41:39

问题


Background:

I am running a cross-platform Xamarin.Forms app on a Nexus 7 (Android API level 22) that is configured to (in order):

  1. Connect to my WiFi network
  2. Connect to a Python script that runs on the router/gateway (10.0.0.0/5000)
  3. Connect to an A2DP Bluetooth audio receiver

on startup.

  • My development machine is running Windows 7 with Visual Studio 2019 Preview (v16.0.0 Preview 1.1), Xamarin.Forms (v3.4.0.1008975), and Xamarin.Android (v28.0.0).
  • The WiFi network is hosted by a Raspberry Pi 3 B+ running hostapd and dnsmasq on Raspbian Stretch. This access point hands out IP addresses in the range of 10.0.0.1 to 10.0.0.20, with a default gateway of 10.0.0.0. The network currently does not have Internet, but I may add it in the future.
  • The Pi also runs a script in Python which acts as a server for this app. The app is supposed to connect to this script via the .NET TcpClient so it can receive sensor data from the Pi.

The issue:

The app successfully connects to the WiFi and Bluetooth (steps 1 and 3), but I am getting a System.Net.Sockets.SocketException (HRESULT: 0x80004005) when it attempts to connect to the Python script (step 2). Here is the stack trace:

{System.Net.Sockets.SocketException (0x80004005): Network is unreachable
at System.Net.Sockets.SocketAsyncResult.CheckIfThrowDelayedException () 
[0x00014] in <165f7048c1ba483e8b4fcc215c691e9e>:0 
at System.Net.Sockets.Socket.EndConnect (System.IAsyncResult asyncResult) 
[0x0002c] in <165f7048c1ba483e8b4fcc215c691e9e>:0 
at System.Net.Sockets.TcpClient.EndConnect (System.IAsyncResult asyncResult) 
[0x0000c] in <165f7048c1ba483e8b4fcc215c691e9e>:0 
at System.Threading.Tasks.TaskFactory`1[TResult].FromAsyncCoreLogic 
(System.IAsyncResult iar, System.Func`2[T,TResult] endFunction, 
System.Action`1[T] endAction, System.Threading.Tasks.Task`1[TResult] 
promise, System.Boolean requiresSynchronization) [0x00019] in 
<76298308b9444eca9d8db328b3554473>:0 
--- End of stack trace from previous location where exception was thrown ---
at Liberty_Console.Models.Statics+<ConnectAsync>d__2.MoveNext () [0x00052] 
in C:\Users\Micah\Source\Repos\Liberty Console\Liberty Console\Liberty 
Console\Models\Statics.cs:27 }

And this is the code that's causing it:

TcpClient client = new TcpClient(AddressFamily.InterNetwork);
await client.ConnectAsync("10.0.0.0", 5000); //System.Net.Sockets.SocketException

I have:

  • Ensured the app requests and receives these permissions:
    • android.permission.ACCESS_NETWORK_STATE
    • android.permission.ACCESS_WIFI_STATE
    • android.permission.BLUETOOTH
    • android.permission.BLUETOOTH_ADMIN
    • android.permission.CHANGE_NETWORK_STATE
    • android.permission.CHANGE_WIFI_STATE
    • android.permission.INTERNET
  • Ensured the tablet connects to the WiFi network and receives a valid IP address (10.0.0.X)
  • Ensured port 5000 is open on the router/gateway
  • Ensured the Python script is running properly (debugged from a Console App)
  • Tried pinging the router/gateway using the System.Net.NetworkInformation.Ping class
    • The request timed out
  • Researched the issue for a couple hours (to no avail)

My deduction:

Based on my tests and debugging, it seems like the app is unable to make requests on the network (hence it is "unreachable"). This may be due to:

  • How the WiFi network and subnet is structured (i.e. maybe the Android app is trying to run on 192.168.1.X instead of 10.0.0.X)
  • The fact that WiFi network does not have an Internet connection (so clients are limited to local networking)
  • An outgoing firewall/security measure in Android, Xamarin, or on the device itself
  • Differences between the TcpClient in Xamarin/Android and in the regular .NET framework

Any help is greatly appreciated!

Let me know if the question needs more detail or clarification.

来源:https://stackoverflow.com/questions/54034134/xamarin-forms-tcpclient-connection-fails-with-system-net-sockets-socketexcepti

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!