如何获取本机当前网络流量?

s_phoenix 2002-06-17 05:23:54
在这个版搜索了一下,可找到的都不能解决。
谁有解决的办法?最好是现成的代码。
万分感谢!
...全文
374 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
s_phoenix 2002-06-18
  • 打赏
  • 举报
回复
http://www.microsoft.com/msdownload/platformsdk/sdkupdate/
找到了,谢谢 xuying()
s_phoenix 2002-06-18
  • 打赏
  • 举报
回复
谢谢了!
还是想办法更新SDK吧,用了你的两个文件,可是又包含了其他文件:
#include <ipexport.h>
#include <iptypes.h>
呵呵。总不好一直这样循环麻烦你吧。
更新sdk需要从什么地方下载?谢谢!
xuying 2002-06-18
  • 打赏
  • 举报
回复
最好更新MS SDK。
从我的主页上下载吧。
http://www.nlsde.buaa.edu.cn/~xuying/iphlpapi.h
http://www.nlsde.buaa.edu.cn/~xuying/iphlpapi.lib
s_phoenix 2002-06-18
  • 打赏
  • 举报
回复
Header: Declared in Iphlpapi.h.
Library: Use Iphlpapi.lib.
这是GetIfEntry要求的,可是我机器上没有Iphlpapi.h,也没有Iphlpapi.lib,需要装什么(更新sdk?)才有吗?从哪里可以得到?
万分感谢!
xuying 2002-06-17
  • 打赏
  • 举报
回复
你拨号完毕后,用GetIfTable,就会多一条记录。

dwIndex未必是从0开始,也未必是连续的。我的2000上loopback的Index是1, 网卡的Index是10000多。具体得到index的方法看我上面给出的例程。

这个GetIfTable, GetIfEntry其实就是获得SNMP的Interface组的数据。
你看看SNMP的书就知道了。
s_phoenix 2002-06-17
  • 打赏
  • 举报
回复
如果是通过modem拨号连接的呢?怎么读取网络流量?dwIndex给多少?
dwIndex
Specifies the index that identifies the interface.
msdn仅仅这样说,我看你的帖子,dwIndex是从0开始的网络接口。但是这个接口仅指网卡吗?modem拨号连接这种怎么算?
谢谢!
xuying 2002-06-17
  • 打赏
  • 举报
回复
MIB_IFTABLE* ifTable = new MIB_IFTABLE[6]; // 假设最多6个网络接口
MIB_IFROW ifMib;

DWORD dwSize = sizeof(MIB_IFTABLE)*6;
GetIfTable(ifTable, &dwSize, TRUE); // 得到接口列表

ifMib.dwIndex = ifTable->table[0].dwIndex ; // 设定index,此处取第一块网卡
GetIfEntry(&ifMib); // 获取接口的各种信息
xuying 2002-06-17
  • 打赏
  • 举报
回复
MIB_IFROW ifMib;
ifMib.dwIndex = 0; // 如果你的网卡是第一个网络接口。
GetIfEntry(&ifMib); // 调用完毕后,就会将ifMib中的其它成员数据添好了


参考MSDN。
xuying 2002-06-17
  • 打赏
  • 举报
回复
MIB_IFROW ifMib;
ifMib.dwIndex = 0; // 如果你的网卡是第一个网络接口。
GetIfEntry(&ifMib); // 调用完毕后,就会将ifMib中的其它成员数据添好了


参考MSDN。
s_phoenix 2002-06-17
  • 打赏
  • 举报
回复
typedef struct _MIB_IFROW {
WCHAR wszName[MAX_INTERFACE_NAME_LEN];
DWORD dwIndex; // index of the interface
DWORD dwType; // type of interface
DWORD dwMtu; // max transmission unit
DWORD dwSpeed; // speed of the interface
DWORD dwPhysAddrLen; // length of physical address
BYTE bPhysAddr[MAXLEN_PHYSADDR]; // physical address of adapter
DWORD dwAdminStatus; // administrative status
DWORD dwOperStatus; // operational status
DWORD dwLastChange; // last time operational status changed
DWORD dwInOctets; // octets received
DWORD dwInUcastPkts; // unicast packets received
DWORD dwInNUcastPkts; // non-unicast packets received
DWORD dwInDiscards; // received packets discarded
DWORD dwInErrors; // erroneous packets received
DWORD dwInUnknownProtos; // unknown protocol packets received
DWORD dwOutOctets; // octets sent
DWORD dwOutUcastPkts; // unicast packets sent
DWORD dwOutNUcastPkts; // non-unicast packets sent
DWORD dwOutDiscards; // outgoing packets discarded
DWORD dwOutErrors; // erroneous packets sent
DWORD dwOutQLen; // output queue length
DWORD dwDescrLen; // length of bDescr member
BYTE bDescr[MAXLEN_IFDESCR]; // interface description
} MIB_IFROW,*PMIB_IFROW;

我不太明白,如何定义pIfRow呢?
里面的 WCHAR wszName[MAX_INTERFACE_NAME_LEN];这些我怎么给呢?
xuying 2002-06-17
  • 打赏
  • 举报
回复
用GetIfEntry()函数可以获得每一块网卡的流入流出信息。

GetIfEntry
The GetIfEntry function retrieves information for the specified interface on the local computer.

DWORD GetIfEntry(
PMIB_IFROW pIfRow // pointer to interface entry
);
Parameters
pIfRow
[in] Pointer to a MIB_IFROW structure that receives information for an interface on the local computer. Set the dwIndex member of MIB_IFROW to the index of the interface for which to retrieve information.
Return Values
If the function succeeds, the return value is NO_ERROR.

If the function fails, use FormatMessage to obtain the message string for the returned error.

18,363

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧