Cannot telnet to Xamarin Android app that runs as a server

那年仲夏 提交于 2019-12-24 11:29:59

问题


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

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