【问题标题】:How to log network bandwidth on Windows 7?如何在 Windows 7 上记录网络带宽?
【发布时间】:2019-07-12 17:58:25
【问题描述】:

[在谷歌上搜索了几个小时/几天后,我不敢相信这样的基本任务没有一个开箱即用的“hello world”。]

Windows 7 上,如何记录网络统计信息?如果可能,请使用特定的 IP 地址。 此处的目标是在几个小时内记录 UDP/TCP 带宽和错误

已经尝试了很多软件,但没有一个可以工作。例如,NetMon 无法成功启动捕获,即使以管理员身份启动也是如此。

欢迎使用编程解决方案,尤其是使用 C/C++/C#。

【问题讨论】:

  • 使用GetIpStatistics()WMI。见stackoverflow.com/questions/221181/…
  • stackoverflow.com 不是免费的代码设计服务。请发布您尝试过的内容以及它无法执行所需任务的原因。
  • @user3629249 问题正是因为谷歌数小时没有结果。每个人都可以尝试一下,并找出有关此主题的文档不足。这个问题本质上是关于获得一些直接有用的指针。
  • 使用来自docs.microsoft.com/en-us/windows/desktop/api/iphlpapi/… 的示例代码。只需复制粘贴编译并运行!
  • 我明白了,我了解到您对 C 代码感兴趣。无论如何,在 C 语言中,您还可以使用 GetUdpStatistics() 检索特定于 UDP docs.microsoft.com/en-us/windows/desktop/api/iphlpapi/…) 的统计信息,或使用 GetTcpStatistics() 检索 TCP (docs.microsoft.com/en-us/windows/desktop/api/iphlpapi/…) 的统计信息。只需在GetIpStatistics() 的示例代码中替换它们即可。还有许多其他功能可以提供有趣的信息。 我可以建议您更深入地查看我向您发出信号的页面吗?.

标签: c windows winapi logging network-programming


【解决方案1】:

这是 C# 中的工作代码:

using System;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;

namespace App1
{
class Wrap
{
    public static void ShowUdpStatistics(NetworkInterfaceComponent version)
    {
        IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
        UdpStatistics udpStat = null;

        switch (version)
        {
            case NetworkInterfaceComponent.IPv4:
                udpStat = properties.GetUdpIPv4Statistics();
                Console.WriteLine("UDP IPv4 Statistics");
                break;
            case NetworkInterfaceComponent.IPv6:
                udpStat = properties.GetUdpIPv6Statistics();
                Console.WriteLine("UDP IPv6 Statistics");
                break;
            default:
                throw new ArgumentException("version");
                //    break;
        }
        Console.WriteLine("  Datagrams Received ...................... : {0}",
            udpStat.DatagramsReceived);
        Console.WriteLine("  Datagrams Sent .......................... : {0}",
            udpStat.DatagramsSent);
        Console.WriteLine("  Incoming Datagrams Discarded ............ : {0}",
            udpStat.IncomingDatagramsDiscarded);
        Console.WriteLine("  Incoming Datagrams With Errors .......... : {0}",
            udpStat.IncomingDatagramsWithErrors);
        Console.WriteLine("  UDP Listeners ........................... : {0}",
            udpStat.UdpListeners);
        Console.WriteLine("");
    }

    /*public static void SendUdp()
    {
        Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp);

        IPAddress serverAddr = IPAddress.Parse("192.168.2.255");

        IPEndPoint endPoint = new IPEndPoint(serverAddr, 11050);

        string text = "Hello";
        byte[] send_buffer = Encoding.ASCII.GetBytes(text);

        sock.SendTo(send_buffer, endPoint);
    }*/

}

class Program
{
    static void Main(string[] args)
    {
        for (int i=0; i < 10; i++)
        {
            Wrap.ShowUdpStatistics(NetworkInterfaceComponent.IPv4);
            /*Wrap.SendUdp();*/
        }
    }
}
}

这是 MSDN 文档中 example 的增强版(开箱即用!)。

此处添加了注释代码,以验证计数器在发送 UDP 数据包时是否增长。感谢this SO question的第一个回答。

【讨论】:

    【解决方案2】:

    MS 提供了一组 IP 帮助函数 (https://docs.microsoft.com/en-us/windows/desktop/api/_iphlp/),允许管理和监控整个 IP 堆栈(本地 IP 协议和派生的)。

    您可以使用专门针对 IPTCPUDP 的监控功能:

    • GetIpStatistics()
    • GetUdpStatistics()
    • GetTcpStatistics()

    MS 在https://docs.microsoft.com/en-us/windows/desktop/api/iphlpapi/nf-iphlpapi-getipstatistics 中提供了一个功能样本,我报告了:

    #ifndef UNICODE
    #define UNICODE
    #endif
    
    #include <winsock2.h>
    #include <ws2tcpip.h>
    #include <iphlpapi.h>
    #include <stdio.h>
    
    #pragma comment(lib, "iphlpapi.lib")
    
    #define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
    #define FREE(x) HeapFree(GetProcessHeap(), 0, (x))
    
    /* Note: could also use malloc() and free() */
    
    int main()
    {
    
        DWORD dwRetval;
        MIB_IPSTATS *pStats;
    
        pStats = (MIB_IPSTATS *) MALLOC(sizeof (MIB_IPSTATS));
    
        if (pStats == NULL) {
            wprintf(L"Unable to allocate memory for MIB_IPSTATS\n");
            exit(1);
        }
        dwRetval = GetIpStatistics(pStats);
        if (dwRetval != NO_ERROR) {
            wprintf(L"GetIpStatistics call failed with %d\n", dwRetval);
            exit(1);
        } else {
    
            wprintf(L"IP forwarding: \t\t" );
            switch (pStats->dwForwarding) {
            case MIB_IP_FORWARDING: 
                wprintf(L"Enabled\n");
                break;
            case MIB_IP_NOT_FORWARDING: 
                wprintf(L"Disabled\n");
                break;
            default: 
                wprintf(L"unknown value = %d\n", pStats->dwForwarding);
                break;
            }
    
            wprintf(L"Default initial TTL: \t\t\t\t\t%u\n", pStats->dwDefaultTTL);
    
            wprintf(L"Number of received datagrams: \t\t\t\t%u\n", pStats->dwInReceives);
            wprintf(L"Number of received datagrams with header errors: \t%u\n", pStats->dwInHdrErrors);
            wprintf(L"Number of received datagrams with address errors: \t%u\n", pStats->dwInAddrErrors);
    
            wprintf(L"Number of datagrams forwarded: \t\t\t\t%ld\n", pStats->dwForwDatagrams);
    
            wprintf(L"Number of received datagrams with an unknown protocol: \t%u\n", pStats->dwInUnknownProtos);
            wprintf(L"Number of received datagrams discarded: \t\t%u\n", pStats->dwInDiscards);
            wprintf(L"Number of received datagrams delivered: \t\t%u\n", pStats->dwInDelivers);
    
            wprintf(L"Number of outgoing datagrams requested to transmit: \t%u\n", pStats->dwOutRequests);
            wprintf(L"Number of outgoing datagrams discarded for routing: \t%u\n", pStats->dwRoutingDiscards);
            wprintf(L"Number of outgoing datagrams discarded: \t\t%u\n", pStats->dwOutDiscards);
            wprintf(L"Number of outgoing datagrams with no route to destination discarded: %u\n", pStats->dwOutNoRoutes);
    
            wprintf(L"Fragment reassembly timeout: \t\t\t\t%u\n", pStats->dwReasmTimeout);
            wprintf(L"Number of datagrams that required reassembly: \t\t%u\n", pStats->dwReasmReqds);
            wprintf(L"Number of datagrams successfully reassembled: \t\t%u\n", pStats->dwReasmOks);
            wprintf(L"Number of datagrams that could not be reassembled: \t%u\n", pStats->dwReasmFails);
    
            wprintf(L"Number of datagrams fragmented successfully: \t\t%u\n", pStats->dwFragOks);
            wprintf(L"Number of datagrams not fragmented and discarded: \t%u\n", pStats->dwFragFails);
            wprintf(L"Number of fragments created: \t\t\t\t%u\n", pStats->dwFragCreates);
    
            wprintf(L"Number of interfaces: \t\t\t\t\t%u\n", pStats->dwNumIf);
            wprintf(L"Number of IP addresses: \t\t\t\t%u\n", pStats->dwNumAddr);
            wprintf(L"Number of routes: \t\t\t\t\t%u\n", pStats->dwNumRoutes);
        }
    
    // Free memory allocated for the MIB_IPSTATS structure
        if (pStats)
            FREE(pStats);
    
        return 0;
    }
    

    通过将辅助函数替换为适当的函数,此代码可用于与 IP、TCP 和 UDP 相关的每个函数。

    还存在扩展版本,用于那些允许将监控限制为 IPV4 或 IPV6 的功能。

    IP helper 的 MS 页面上提供了详细信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-21
      • 2017-08-22
      • 1970-01-01
      • 2013-12-23
      • 2012-02-01
      • 1970-01-01
      • 2016-10-25
      • 1970-01-01
      相关资源
      最近更新 更多