【问题标题】:How to make sockets work in xamarin?如何使套接字在 xamarin 中工作?
【发布时间】:2018-11-14 08:03:22
【问题描述】:

在此问题的答案中:Server Client Application with .NET and Xamarin 回答的人说:“在 Xamarin.Android 上,您可以使用所有常规的 .Net 套接字类” 我尝试使用 Microsoft 文档示例中的代码,但没有出现任何错误,但应用程序只是在手机上显示:

如果我删除套接字代码,它会正常显示页面。我的代码后面:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Xamarin.Forms;
    using Xamarin.Forms.Xaml;
    using System.Net;
    using System.Net.Sockets;

    namespace App14
     {
     [XamlCompilation(XamlCompilationOptions.Compile)]
     public partial class Page2 : ContentPage
     {
    public static string data = null;

    public static void StartListening()
    {
        // Data buffer for incoming data.  
        byte[] bytes = new Byte[1024];


        // Establish the local endpoint for the socket.  
        // Dns.GetHostName returns the name of the   
        // host running the application.  
        IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
        IPAddress ipAddress = ipHostInfo.AddressList[0];
        IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);

        // Create a TCP/IP socket.  
        Socket listener = new Socket(ipAddress.AddressFamily,
            SocketType.Stream, ProtocolType.Tcp);

        try
        {
            listener.Bind(localEndPoint);
            listener.Listen(10);


            // Start listening for connections.  
            while (true)
            {
                var label = new Label() { Text = "searching for a connection" };
                // Program is suspended while waiting for an incoming connection.  
                Socket handler = listener.Accept();
                label.Text = "Found a Connection";
                data = null;

                // An incoming connection needs to be processed.  
                while (true)
                {
                    int bytesRec = handler.Receive(bytes);
                    data += Encoding.ASCII.GetString(bytes, 0, bytesRec);
                    if (data.IndexOf("<EOF>") > -1)
                    {
                        break;
                    }
                }

                // Show the data on the console.  
                label.Text = "Text received" + data;

                // Echo the data back to the client.  
                byte[] msg = Encoding.ASCII.GetBytes(data);

                handler.Send(msg);
                handler.Shutdown(SocketShutdown.Both);
                handler.Close();
            }

        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }


    }

    public Page2 ()
    {
        StartListening();
        Title = "Sign in page";
        InitializeComponent();

    }
   private void page2_click(object sender,EventArgs e)
    {
        Navigation.PushAsync(new Page1(), true);
    }
}
}

Xaml 文件代码:

为什么会这样?您能否提供一个客户端移动或服务器移动套接字的示例

【问题讨论】:

标签: sockets xamarin


【解决方案1】:
  1. 您正在使用 StartListening 调用阻塞 UI 线程,因为它有一个无限的 while 循环。
  2. 您在 while 循环中创建的标签永远不会作为内容添加到页面上。因此,您添加到其中的文本将永远不会显示。您已经在页面上定义了名称为 l 的标签。在这种情况下,您可以使用它来添加文本:

    l.Text = "hello";
    
  3. 正如评论,您应该在另一个线程上启动您的套接字侦听代码,以免阻塞您的 UI。这可能就像写Task.Run(() =&gt; StartListening());一样简单。确保您了解它的作用,以及在离开页面时或在应用生命周期期间如何再次取消此 Task

【讨论】:

  • 虽然我使用了此代码,但我更改了地址列表[0] ro addresslist[1](ipv4) UI 可能工作但我尝试使用移动设备的 IP 地址从我的计算机连接它没有根本不工作?!!!
【解决方案2】:

如果您在两台设备位于同一 LAN 时尝试使用正确的侦听器 IP 地址和端口号从您的移动设备进行连接,它应该可以工作。如果其中一个设备位于不同的 LAN 路由器/调制解调器后面,您将无法连接,因为路由器将阻止所有传入连接,除非您使用 UPnP 等端口路由机制。

【讨论】:

    猜你喜欢
    • 2010-09-08
    • 1970-01-01
    • 2020-11-02
    • 2016-11-10
    • 1970-01-01
    • 2021-05-11
    • 2010-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多