[在谷歌搜索并搜索了数小时/天之后,我不敢相信这样的基本任务没有开箱即用的“ hello world”。]
在 Windows 7 上,如何记录网络统计信息?如果可能,请使用特定的IP地址。 目的是在几个小时内记录UDP / TCP带宽和错误。
已经尝试了很多软件,但是没有一个工作。例如,即使以管理员身份启动捕获,NetMon也没有成功启动捕获。
欢迎使用编程解决方案,尤其是使用C / C ++ / C#时。
答案 0 :(得分:1)
MS提供了一组IP帮助器功能(https://docs.microsoft.com/en-us/windows/desktop/api/_iphlp/),该功能可以管理和监视整个IP堆栈(本机IP协议及其派生的)。
您可以使用专门用于 IP , TCP 或 UDP 的监视功能:
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助手的MS页面上提供了详细信息。
答案 1 :(得分:0)
这是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的第一个答案。