كيفية تحديد ما إذا كان عنوان IP من نفس الشبكة المحلية برمجيا في .NET C#

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

  •  03-07-2019
  •  | 
  •  

سؤال

وأنا أعلم أنه إذا IP يقع خارج الشبكة الفرعية قناع + IP المحلي القواعد ، وسوف يكون فقط يمكن الوصول إليها من خلال البوابة.المشكلة هي أنني لا أعرف كيفية الحصول على عنوان IP المحلي, لا المحلية قناع الشبكة الفرعية ، برمجيا باستخدام .صافي.أي يمكنك مساعدتي ؟

وسوف تستخدم هذه المعلومات للضغط على أقصى قدر من الأداء من دفعة SQL الإدراج في قائمة الانتظار.إذا كان ملقم SQL يقع في نفس الشبكة الفرعية ، ثم فإنه سيتم استخدام خوارزمية الأمثل من أجل الحد الأدنى للعمر الكمون ، وإلا سوء استخدام أحد الأمثل استتار عالية.

هل كانت مفيدة؟

المحلول

ويمكنك استخدام الفئات داخل مساحة System.Net.NetworkInformation (عرض في .NET Framework 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
                    );
            }
        }

نصائح أخرى

هناك طريقة بديلة باستخدام 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, المبسطة فقط لاظهار المعلومات التي تحتاج على الأرجح.

تحرير:استغرق مني وقتا طويلا (الكثير من الأشياء في نفس الوقت :) ) لجعل هذا المنصب ، ميتش سبقتنى :)

وهذا سوف تحصل على اسم المضيف وعنوان IP. أفترض أنك تعرف المتكاملة في LAN الخاصة بك لذلك يجب عليك أن تكون قادرا على تحديد ما إذا كان عنوان 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