How to get the IP address of the current host in C#

solution

Get hostname

The Dns class provides a set of static methods for Domain Name System (DNS) operations.

Dns.GetHostName();

We can use the Dns.GetHostName() method to get the host name of the local computer. This is a static method. This method calls the operating system's network stack to obtain the computer's hostname. A hostname is a name used to identify a device on a network. In the network, each device has a unique host name, and communication between devices can be carried out on the network through the host name.

Get IP address

Dns.GetHostAddresses(hostName);

After obtaining the host name, we use the Dns.GetHostAddresses(hostName) method to obtain the IP list. This is also a static method used to obtain the IP address list corresponding to the specified host name (or IP address string). It accepts a hostname or IP address as a parameter and returns an IPAddress[] array containing all IP addresses associated with the hostname or IP address.

The method first attempts to resolve the hostname passed in the parameter. If a valid hostname is passed in, it will query the DNS server to find the IP address corresponding to that hostname. If the relevant IP addresses are found, an array containing these IP addresses will be returned.

In addition, if the incoming parameter is a valid IP address string (for example: "192.168.0.1"), then the method will directly parse the string into an IPAddress object and put it into the array and return it.

During the parsing process, this method will throw an exception, such as SocketException or SecurityException, if it encounters problems or access restrictions.

Filter IPv4 addresses

IPv4 addresses can be filtered through an enumeration value AddressFamily.InterNetwork.

AddressFamily is an enumeration type that defines different network address types. Among them, InterNetwork represents the IPv4 address, InterNetworkV6 represents the IPv6 address, and Unix represents the Unix domain socket address, etc.

function

using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System;

public static List<string> GetIPv4Addresses()
{
    
    
    string hostName = Dns.GetHostName();
    IPAddress[] addresses = Dns.GetHostAddresses(hostName);

    List<string> ipv4Addresses = new List<string>();

    foreach (IPAddress address in addresses)
    {
    
    
        // 判断是否为IPv4地址
        if (address.AddressFamily == AddressFamily.InterNetwork)
        {
    
    
            ipv4Addresses.Add(address.ToString());
        }
    }

    return ipv4Addresses;
}

This function will return all IPv4 addresses found. After calling, you will get a list containing all IPv4 addresses found. If no IPv4 address is found, an empty list is returned.

function call

Print out all IPv4 addresses found.
The function call is as follows:

List<string> ipv4Addresses = GetIPv4Addresses();

foreach (string address in ipv4Addresses)
{
    
    
    Console.WriteLine(address);
}

Guess you like

Origin blog.csdn.net/weixin_44499065/article/details/132311154