问题
I have created a Xamarin Forms app that implements the code found here to setup an endpoint: https://docs.microsoft.com/en-us/dotnet/framework/network-programming/asynchronous-server-socket-example
I have added INTERNET permission in AndroidManifest.xml
as follows:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.Server">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="27" />
<uses-permission android:name="android.permission.INTERNET" />
<application android:label="Server.Android"></application>
</manifest>
I then call the StartListening
method from the default MainActivity.cs
file as follows:
using System;
using System.Threading;
using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace Server.Droid
{
[Activity(Label = "Server", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
new Thread(new ThreadStart(delegate {
AsynchronousSocketListener.StartListening();
})).Start();
}
}
}
I have stepped through the debugger and I see that the endpoint gets created. I am now trying to telnet to the app via the IP address
I see is assigned in this line of code in StartListening
:
IPAddress ipAddress = ipHostInfo.AddressList[0];
C:\> telnet 10.x.x.x 11000
I always get the error message
Could not open connection to the host, on port 11000: Connect failed
I have tried turning Windows firewall on and off to no avail. I have verified my app is listening on port 11000 by using Netstat for Android running on the emulator.
来源:https://stackoverflow.com/questions/54622085/cannot-telnet-to-xamarin-android-app-that-runs-as-a-server