简体   繁体   English

如何获取在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

I need to get the IP address of a Wi-Fi module, running a TCP server. 我需要获取运行TCP服务器的Wi-Fi模块的IP地址。 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 . 该应用程序将打开WiFi连接设置页面,以允许用户连接到Wi-Fi模块创建的网络(要求输入密码)- 参见所附图片 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). 服务器(Wi-FI模块)的IP地址是172.1.4.155(例如),但是当我尝试使用GetLocalIPAddress() (在下面附上)在Xamarin.Forms中获取IP地址时,它返回的地址是设备(电话)-172.1.4.55(例如)。 I need to be able to get the IP address programmatically without a user input in an Application. 我需要能够以编程方式获取IP地址,而无需在应用程序中输入用户信息。

WiFi连接设置页面示例

Is there any way to get the IP address of the (external) non-device specific IP address? 有什么方法可以获取(外部)非设备特定IP地址的IP地址? I assume the returned IP address of the phone is the DHCP assigned IP address. 我假设电话的返回IP地址是DHCP分配的IP地址。 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. 我需要获取服务器IP地址,因为在电话和WiFi模块之间建立TCP套接字连接至关重要。 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. 几天来我一直在寻找没有成功的解决方案,因此,我们将非常感谢您的帮助/建议或示例。

The code below is the GetLocalIPAddress() function to get the IP address. 下面的代码是GetLocalIPAddress()函数,用于获取IP地址。

    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";
    }

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. 如何以XAMARIN格式读取IP地址,我已经在IOS和Android项目中创建了DependencyServices,并获得了正确的IP地址。 Below is code to get IP address. 下面是获取IP地址的代码。

In PCL Project 在PCL项目中

public interface IIPAddressManager
    {
        String GetIPAddress();
    }

In IOS Project 在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;
        }
    }
}

In Android Project. 在Android项目中。

[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;
            }
        }
    }
}

Then call a DependencyServices in UI project. 然后在UI项目中调用DependencyServices。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM