如何获得在Xamarin.Forms中的wifi模块上创建的服务器(不是Web服务器)的IP地址,而是一个TCP服务器 [英] How do you get the IP address of a server (not a webserver) but a TCP server created on a wifi module in Xamarin.Forms

查看:413
本文介绍了如何获得在Xamarin.Forms中的wifi模块上创建的服务器(不是Web服务器)的IP地址,而是一个TCP服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取运行TCP服务器的Wi-Fi模块的IP地址.该应用程序将打开"WiFi连接设置"页面,以允许用户连接到Wi-Fi模块创建的网络(要求输入密码)-请参阅所附图片.服务器(Wi-FI模块)的IP地址是172.1.4.155(例如),但是当我尝试使用 GetLocalIPAddress() (附加在下面),它返回的地址是设备(电话)的本地IP地址-172.1.4.55(例如).我需要能够以编程方式获取IP地址,而无需在应用程序中输入用户信息.

I need to get the IP address of a Wi-Fi module, running a TCP server. The application will open the WiFi connections settings page to allow the user to connect to the Wi-Fi module's created network (requiring the password to be entered)- see attached picture. The server (Wi-FI module) IP address is 172.1.4.155 (for example) but when I try to get the IP address in Xamarin.Forms using GetLocalIPAddress() (attached below), the address it returns is the local IP address of the device (Phone)- 172.1.4.55 (for example). I need to be able to get the IP address programmatically without a user input in an Application.

有什么方法可以获取(外部)非设备特定IP地址的IP地址?我假设电话返回的IP地址是DHCP分配的IP地址.我需要获取服务器IP地址,因为在电话和WiFi模块之间建立TCP套接字连接是必不可少的.几天以来,我一直在寻求没有成功的解决方案,因此,我们将不胜感激任何帮助/建议或示例.

Is there any way to get the IP address of the (external) non-device specific IP address? I assume the returned IP address of the phone is the DHCP assigned IP address. I need to get the server IP address as it is essential to establish a TCP socket connection between the phone and the WiFi module. I have been trying to look for a solution without any success for a few days now so any help/suggestions or examples will be greatly appreciated.

下面的代码是 GetLocalIPAddress()函数,用于获取IP地址.

The code below is the GetLocalIPAddress() function to get the IP address.

    public static string GetLocalIPAddress()
    {
        var host = Dns.GetHostEntry(Dns.GetHostName());
        foreach (var ip in host.AddressList)
        {
            if (ip.AddressFamily == AddressFamily.InterNetwork)
            {
                return ip.ToString();
            }
            //return "";
        }
        //throw new Exception("Local IP Address Not Found!");
        return "None";
    }

推荐答案

如何以XAMARIN格式读取IP地址,我已经在IOS和Android项目中创建了DependencyServices,并获得了正确的IP地址.下面是获取IP地址的代码.

How to Read IP Address in XAMARIN Form, i have Created the DependencyServices in both IOS and Android projects and getting a proper IP address. Below is code to get IP address.

在PCL项目中

public interface IIPAddressManager
    {
        String GetIPAddress();
    }

在IOS项目中

[assembly: Dependency(typeof(YourAppNamespace.iOSUnified.iOS.DependencyServices.IPAddressManager))]

namespace YourAppNamespace.iOSUnified.iOS.DependencyServices
{
    class IPAddressManager : IIPAddressManager
    {
        public string GetIPAddress()
        {
            String ipAddress = "";

            foreach (var netInterface in NetworkInterface.GetAllNetworkInterfaces())
            {
                if (netInterface.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 ||
                    netInterface.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
                {
                    foreach (var addrInfo in netInterface.GetIPProperties().UnicastAddresses)
                    {
                        if (addrInfo.Address.AddressFamily == AddressFamily.InterNetwork)
                        {
                            ipAddress = addrInfo.Address.ToString();

                        }
                    }
                }
            }

            return ipAddress;
        }
    }
}

在Android项目中.

In Android Project.

[assembly: Dependency(typeof(YourAppNamespace.Android.Android.DependencyServices.IPAddressManager))]

namespace YourAppNamespace.Android.Android.DependencyServices
{
    class IPAddressManager : IIPAddressManager
    {
        public string GetIPAddress()
        {
            IPAddress[] adresses = Dns.GetHostAddresses(Dns.GetHostName());

            if (adresses !=null && adresses[0] != null)
            {
                return adresses[0].ToString();
            }
            else
            {
                return null;
            }
        }
    }
}

然后在UI项目中调用DependencyServices.

Then call a DependencyServices in UI project.

string ipaddress = DependencyService.Get<IIPAddressManager>().GetIPAddress

这篇关于如何获得在Xamarin.Forms中的wifi模块上创建的服务器(不是Web服务器)的IP地址,而是一个TCP服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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