.NET C#에서 IP가 프로그래밍 방식으로 동일한 LAN에서 IP인지 확인하는 방법

StackOverflow https://stackoverflow.com/questions/416524

  •  03-07-2019
  •  | 
  •  

문제

IP가 서브넷 마스크 + 로컬 IP 규칙을 벗어나면 게이트웨이를 통해서만 도달 할 수 있다는 것을 알고 있습니다. 문제는 .NET을 사용하여 로컬 IP 주소 인 로컬 IP 주소를 얻지 못하는 방법을 모른다는 것입니다. 당신이 나를 도울 수 있습니까?

이 정보를 사용하여 배치 SQL 삽입 큐에서 최대 성능을 짜겠습니다. SQL 서버가 동일한 서브넷에 속하는 경우 미니 늄 대기 시간에 최적화 된 알고리즘을 사용하면 높은 대기 시간에 최적화 된 하나를 사용합니다.

도움이 되었습니까?

해결책

System.net.networkInformation 네임 스페이스 내부의 클래스를 사용할 수 있습니다 (.NET 2.0에서 소개) :

        NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();

        foreach (NetworkInterface iface in interfaces)
        {
            IPInterfaceProperties properties = iface.GetIPProperties();

            foreach (UnicastIPAddressInformation address in properties.UnicastAddresses)
            {
                Console.WriteLine(
                    "{0} (Mask: {1})",
                    address.Address,
                    address.IPv4Mask
                    );
            }
        }

다른 팁

The를 사용하는 대안적인 방법이 있습니다 NetworkInformation 수업:

public static void ShowNetworkInterfaces()
{
    // IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();
    NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();

    if (nics == null || nics.Length < 1)
    {
        Console.WriteLine("  No network interfaces found.");
        return;
    }

    Console.WriteLine("  Number of interfaces .................... : {0}", nics.Length);
    foreach (NetworkInterface adapter in nics)
    {
        IPInterfaceProperties properties = adapter.GetIPProperties();
        Console.WriteLine();
        Console.WriteLine(adapter.Description);
        Console.WriteLine(String.Empty.PadLeft(adapter.Description.Length,'='));
        Console.WriteLine("  Interface type .......................... : {0}", adapter.NetworkInterfaceType);
        Console.WriteLine("  Physical Address ........................ : {0}", adapter.GetPhysicalAddress().ToString());
        string versions ="";

        // Create a display string for the supported IP versions.
        if (adapter.Supports(NetworkInterfaceComponent.IPv4))
        {
            versions = "IPv4";
        }
        if (adapter.Supports(NetworkInterfaceComponent.IPv6))
        {
            if (versions.Length > 0)
            {
                versions += " ";
            }
            versions += "IPv6";
        }
        Console.WriteLine("  IP version .............................. : {0}", versions);
        UnicastIPAddressInformationCollection uniCast = properties.UnicastAddresses;
        if (uniCast != null)
        {
            foreach (UnicastIPAddressInformation uni in uniCast)
            {
                Console.WriteLine("  Unicast Address ......................... : {0}", uni.Address);
                Console.WriteLine("     Subnet Mask  ......................... : {0}", uni.IPv4Mask);
            }
        }
    Console.WriteLine();
    }
}

코드 샘플은 MSDN이 제공 한 예제 인 매시업 형식이며, 아마도 필요한 정보 만 표시하도록 단순화되었습니다.

편집 :이 게시물을 만들기 위해 너무 오래 걸렸습니다 (동시에 너무 많은 것 :)) Mitch는 저를 이겼습니다 :)

이렇게하면 호스트 이름과 IP 주소가 제공됩니다. LAN의 IP를 알고 있다고 가정하므로 IP 주소가 LAN 외부에 있는지 확인할 수 있어야합니다.

// Get the host name of local machine.
strHostName = Dns.GetHostName();
Console.WriteLine("Local Machine's Host Name: " + strHostName);

// Using the host name, get the IP address list.
IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);
IPAddress[] addr = ipEntry.AddressList;

for (int i = 0; i < addr.Length; i++)
{
     Console.WriteLine("IP Address {0}: {1} ", i, addr[i].ToString());
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top