locked
How to get Machine IPAddress from ubuntu using c# RRS feed

  • Question

  • User-1730441610 posted

    I trying to get IP Address to using below code, but if it's multicast nic how will get

    public static List<string> GetAllLocalIPv4(NetworkInterfaceType type)
    {
    return NetworkInterface.GetAllNetworkInterfaces()
    .Where(x => x.NetworkInterfaceType == type && x.OperationalStatus == OperationalStatus.Up)
    .SelectMany(x => x.GetIPProperties().UnicastAddresses)
    .Where(x => x.Address.AddressFamily == AddressFamily.InterNetwork)
    .Select(x => x.Address.ToString())
    .ToList();
    }
    Tuesday, August 25, 2020 6:56 AM

All replies

  • User753101303 posted

    Hi,

    Do you see the address you want in the current list? Not a network expert but for now is is unclear if you want  https://docs.microsoft.com/en-us/dotnet/api/system.net.networkinformation.ipinterfaceproperties.multicastaddresses?view=netframework-4.8 instead or if you try to add some unknown criteria to select one (and only one ?) IP address.

    If you want the address on which your web site is responding, I would rather look at HttpRequest and maybe ServerVariables. Note that depending on the config, it is possible to have a web site that is responding on multiple IP addresses rather than on a single address.

    Tuesday, August 25, 2020 11:35 AM
  • User-1730441610 posted

    with this example i am getting only a multiCast address i want to get multiCast as well as single IP address?

    Tuesday, August 25, 2020 1:03 PM
  • User-474980206 posted

    On Linux, multicast is implemented at the application level, not the interface. Your app uses use setsocket() to enable multicast on its socket.

    Tuesday, August 25, 2020 3:09 PM
  • User-640622294 posted
     public static string GetIPAddress()
            {
                string ip = new WebClient().DownloadString("https://api.ipify.org") ?? HttpContext.Current.Request.UserHostAddress;
                return ip;
            }

    Tuesday, August 25, 2020 4:10 PM