Dns.GetHostAddress(hostname)在Ubuntu 16.04中没有这样的设备或地址异常 [英] Dns.GetHostAddress(hostname) No such device or address Exception in Ubuntu 16.04

查看:234
本文介绍了Dns.GetHostAddress(hostname)在Ubuntu 16.04中没有这样的设备或地址异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.net core 2.0项目,该项目正在尝试从linux计算机获取主机名和主机IP地址.该程序可以在Mac OS和Windows上很好地运行,但不能在Linux上运行-Ubuntu 16.04

I have a .net core 2.0 project which is trying to get Host name and Host IP address from a linux machine. The program runs well in Mac OS and Windows but not on linux - Ubuntu 16.04

public class Program
{
    public static void Main(string[] args)
    {
        var HostName = Dns.GetHostName();
        Console.WriteLine("Host name : " + HostName);
        var HostAddress = GetHostAddress(HostName);
        Console.WriteLine("Host address : " + HostAddress);
    }

    private static string GetHostAddress(string hostName)
    {
        try
        {
            var addressList = Dns.GetHostAddresses(hostName);

            foreach (IPAddress address in addressList)
            {
                Console.WriteLine("IP Address : " + address.ToString());
                if (address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                {
                    string ip = address.ToString();
                    if (!ip.StartsWith("127."))
                        return ip;
                }
            }

            return "127.0.0.1";
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
        
    }
}

我遇到的例外情况是

ubuntu @ ip-10-40-121-185:〜/home/IP $ dotnet IP.dll

ubuntu@ip-10-40-121-185:~/home/IP$ dotnet IP.dll

主机名:ip-10-40-121-185

Host name : ip-10-40-121-185

System.Net.Internals.SocketExceptionFactory + ExtendedSocketException(0x00000005):没有此类设备或地址

System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (0x00000005): No such device or address

在System.Net.Dns.InternalGetHostByName(字符串hostName,布尔值includeIPv6)

at System.Net.Dns.InternalGetHostByName(String hostName, Boolean includeIPv6)

在System.Net.Dns.GetHostAddresses(字符串hostNameOrAddress)

at System.Net.Dns.GetHostAddresses(String hostNameOrAddress)

在/Users/jliu/RiderProjects/IpTest/IP/Program.cs:line 34中的IP.Program.GetHostAddress(String hostName)

at IP.Program.GetHostAddress(String hostName) in /Users/jliu/RiderProjects/IpTest/IP/Program.cs:line 34

未处理的异常: System.Net.Internals.SocketExceptionFactory + ExtendedSocketException:没有此类设备或地址

Unhandled Exception: System.Net.Internals.SocketExceptionFactory+ExtendedSocketException: No such device or address

在System.Net.Dns.InternalGetHostByName(字符串hostName,布尔值includeIPv6)

at System.Net.Dns.InternalGetHostByName(String hostName, Boolean includeIPv6)

在System.Net.Dns.GetHostAddresses(字符串hostNameOrAddress)

at System.Net.Dns.GetHostAddresses(String hostNameOrAddress)

在/Users/jliu/RiderProjects/IpTest/IP/Program.cs:line 52中的IP.Program.GetHostAddress(String hostName)

at IP.Program.GetHostAddress(String hostName) in /Users/jliu/RiderProjects/IpTest/IP/Program.cs:line 52

在/Users/jliu/RiderProjects/IpTest/IP/Program.cs:line 20中的IP.Program.Main(String [] args) 已中止(核心已弃用)

at IP.Program.Main(String[] args) in /Users/jliu/RiderProjects/IpTest/IP/Program.cs:line 20 Aborted (core dumped)

有什么办法解决它或在Linux机器上获得IP的任何替代方法吗? 谢谢.

Any idea how to fix it or any alternative to get IP on a linux machine? Thanks.

推荐答案

从另一个线程获取答案获取本地适用于我的IP地址

Get answer from another thread Get local IP address which works for me

感谢@Gerardo H的解决方案 https://stackoverflow.com/a/28621250/4861127

Thanks for the solution from @Gerardo H https://stackoverflow.com/a/28621250/4861127

    internal static string GetLocalIPv4(NetworkInterfaceType _type)
    {
        string output = "";
        foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces())
        {
            if (item.NetworkInterfaceType == _type && item.OperationalStatus == OperationalStatus.Up)
            {
                IPInterfaceProperties adapterProperties = item.GetIPProperties();

                if (adapterProperties.GatewayAddresses.FirstOrDefault() != null)
                {
                    foreach (UnicastIPAddressInformation ip in adapterProperties.UnicastAddresses)
                    {
                        if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                        {
                            output = ip.Address.ToString();
                        }
                    }
                }
            }
        }

        return output;
    }

这篇关于Dns.GetHostAddress(hostname)在Ubuntu 16.04中没有这样的设备或地址异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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