【问题标题】:How to get the current MTU of an interface in C#如何在 C# 中获取接口的当前 MTU
【发布时间】:2018-11-22 20:52:13
【问题描述】:

我想向用户显示当前网络接口的 mtu 值。用户可以通过 netsh 命令更改 mtu,例如对于 id 为 11 的接口:

netsh interface ipv4 set subinterface 11 mtu=1700 store=persistent

如何通过 id 或接口名称读取接口的当前 MTU?

如果我使用 System.Net.NetworkInformation 命名空间中的 NetworkInterface 类示例,所有接口的 MTU 为 1500。但使用 netsh 命令(见上文)我得到正确的 MTU 值,例如1700.


这是一个例子:

https://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkinterface(v=vs.110).aspx

public static void ShowNetworkInterfaces()
{
    IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();
    NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
    Console.WriteLine("Interface information for {0}.{1}     ",
            computerProperties.HostName, computerProperties.DomainName);
    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());
        Console.WriteLine("  Operational status ...................... : {0}", 
            adapter.OperationalStatus);
        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);
        ShowIPAddresses(properties);

        // The following information is not useful for loopback adapters.
        if (adapter.NetworkInterfaceType == NetworkInterfaceType.Loopback)
        {
            continue;
        }
        Console.WriteLine("  DNS suffix .............................. : {0}", 
            properties.DnsSuffix);

        string label;
        if (adapter.Supports(NetworkInterfaceComponent.IPv4))
        {
            IPv4InterfaceProperties ipv4 = properties.GetIPv4Properties();
            Console.WriteLine("  MTU...................................... : {0}", ipv4.Mtu);
            if (ipv4.UsesWins)
            {

                IPAddressCollection winsServers = properties.WinsServersAddresses;
                if (winsServers.Count > 0)
                {
                    label = "  WINS Servers ............................ :";
                    ShowIPAddresses(label, winsServers);
                }
            }
        }

        Console.WriteLine("  DNS enabled ............................. : {0}", 
            properties.IsDnsEnabled);
        Console.WriteLine("  Dynamically configured DNS .............. : {0}", 
            properties.IsDynamicDnsEnabled);
        Console.WriteLine("  Receive Only ............................ : {0}", 
            adapter.IsReceiveOnly);
        Console.WriteLine("  Multicast ............................... : {0}", 
            adapter.SupportsMulticast);
        ShowInterfaceStatistics(adapter);

        Console.WriteLine();
    }

【问题讨论】:

  • 有效 MTU 是所有网络组件中最低的 MTU。当你尝试 1700 时你会很快达到,你无法绕过下一步的基本 MTU 限制,以太网在一帧中只能携带 1500 个字节。

标签: c# .net mtu


【解决方案1】:

我自己找到了答案。我还必须更改 ipv6 接口的 MTU,例如:

netsh interface ipv4 set subinterface 11 mtu=1700 store=persistent

netsh interface ipv6 set subinterface 11 mtu=1700 store=persistent

【讨论】:

    【解决方案2】:

    主要问题是问题

    NetworkInterface.GetIPProperties().GetIPv4Properties().Mtu
    NetworkInterface.GetIPProperties().GetIPv6Properties().Mtu
    

    如果启用了 ipv6,两者都返回 ipv6 的 mtu 大小! 只有禁用 ipv6 协议时,Mtu 值才是正确的。

    但正确的值由 netsh 显示

    netsh interface ipv4 show subinterface "Local Area Connection"
    netsh interface ipv6 show subinterface "Local Area Connection"
    

    如果没有协议版本,则会显示 ipv4 的值。 netsh interface ip show subinterface "Local Area Connection"

    首先我想,在启用 ipv6 的情况下,只使用了 ipv6-mtu 大小,但事实并非如此。

    【讨论】:

      猜你喜欢
      • 2018-05-19
      • 2017-02-13
      • 1970-01-01
      • 2016-04-23
      • 2011-07-02
      • 1970-01-01
      • 1970-01-01
      • 2017-02-27
      相关资源
      最近更新 更多