TCP套接字实现在Xamarin.Android中的工作方式是否不同? [英] Does TCP Socket Implementation Work Differently in Xamarin.Android?

查看:347
本文介绍了TCP套接字实现在Xamarin.Android中的工作方式是否不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 System.Net.Sockets.TcpClient 在Xamarin Android应用中建立TCP连接.

I am trying to make a TCP connection in my Xamarin Android app using System.Net.Sockets.TcpClient.

在控制台应用程序上进行连接时,连接非常好.但是,当我在Android应用程序中使用完全相同的代码时,在 tcpClient.Connect("127.0.0.1",6121); 上出现异常,并说"System.Net.Sockets.SocketException(0x80004005)): 连接被拒绝".由于它是Xamarin Android,我需要做些不同的事情吗?下面是我的代码.谢谢您的帮助!

The connection is perfectly fine when I do it on a console application. However, when I used the exact same code in my android app, I get an exception on tcpClient.Connect("127.0.0.1", 6121); saying "System.Net.Sockets.SocketException (0x80004005): Connection refused". Is there something I need to do differently since it's Xamarin Android? Below is my code. Thank you for the help!

using Android.App;
using Android.OS;
using System;
using System.Net.Sockets;

namespace App1
{
    [Activity(Label = "App1", MainLauncher = true)]
    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            Connect();
        }

        public void Connect()
        {
            try
            {
                TcpClient tcpClient = new TcpClient();
                tcpClient.Connect("127.0.0.1", 6121);
            }
            catch(Exception){}
        }
    }
}

推荐答案

无论您是否可以在主线程上进行网络操作(如@ÖmerBaş所评论),网络概念都会导致更基本的问题您的错误.

Regardless of whether or not you can do network operations on the main thread (as commented by @ÖmerBaş,) you have a much more basic problem with network concepts causing your error.

127.0.0.1 总是是指程序在其上运行的计算机.

127.0.0.1 always refers to the machine the program is running on.

当您在控制台中运行代码时,127.0.0.1指的是您正在使用的PC,并且大概是TCP服务器运行的位置.

When you run your code in the console, 127.0.0.1 refers to the PC you are working on and presumably where the TCP server is running.

在Android上运行代码时,127.0.0.1指的是Android设备本身.您的错误消息显示连接被拒绝".那是因为您的TCP-Server不在Android设备上运行.它正在您的PC上运行.

When you run the code on Android, 127.0.0.1 refers to the Android device itself. Your error message says "Connection refused." That's because your TCP-Server isn't running on the Android device. It is running on your PC.

您需要知道您的Android设备可用于连接到PC的IP地址.

You need to know what IP address your Android device can use to connect to your PC.

如果您使用的是真正的Android设备,则需要使Android设备通过WiFi连接到与PC相同的网络.然后,您可以在代码中使用PC的IP地址.

If you are using a real Android device, you will need to have your Android device connect to the same network as your PC via WiFi. Then you can use your PC's IP address in your code.

对于模拟器,有两种可能性:

For the simulators, there's a couple of possibilities:

  1. Google模拟器:10.0.2.2是从Android看到的PC地址.
  2. Microsoft模拟器:169.254.80.80是从Android看到的PC的地址.

(仿真器地址来自此处.)

确保将TCP服务绑定到所有IP地址,而不仅仅是127.0.0.1.

Make sure to bind your TCP service to all IP addresses, not just 127.0.0.1.

另一种解决方案是使用adb在Android设备上设置端口转发(无论是模拟的还是真实的.)

我认为主线程上的网络访问"问题更多是不应该"而不是不能"的问题.ÖmerBaş提到的问题显示了权限被拒绝"错误,而不是指向线程问题的提示.

I think the "network access on the main thread" thing is more a problem of "should not" rather than "cannot." The question referred to by Ömer Baş shows "permission denied" errors rather than something pointing to a threading problem.

这篇关于TCP套接字实现在Xamarin.Android中的工作方式是否不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆