📄 winnetstat.htm
字号:
<HTML>
<HEAD>
<TITLE></TITLE>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 7.0">
</HEAD>
<BODY> <!-- Article Starts - DO NOT ADD HTML/BODY START TAGS-->
<!-- Download Links -->
<UL class="download">
<LI>
<A href="WinNetStat/WinNetStat_src.zip">Download source files - 756 Kb</A>
<LI>
<A href="WinNetStat/WinNetStat_demo.zip">Download demo project - 756 Kb</A></LI>
</UL> <!-- Add the rest of your HTML here -->
<H2>
Introduction
</H2>
<P>
There are few articles which describe how to retrieve the statistics
about network. We used to load the inetmib1.dll manually and map the
function pointers in order to get all the required information.
</P>
<P>
Finally microsoft published some nice APIs to perform these operations.
This article explains how to make use of those published APIs. The APIs are
categorized as follows
</P>
<H3>
<FONT color="#ff9933">Interface Class</FONT>
</H3>
This class allows applications to retrieve network adapter characteristic
information. The IP Helper APIs available for Windows NT 4.0 Service Pack 4 are
listed below:
<UL>
<LI>
GetIfTable: Retrieves all network adapters physical characteristics.
<BR>
<BR>
<LI>
GetIfEntry: Retrieves specific adapter characteristics.
<BR>
<BR>
<LI>
SetIfEntry: Updates a specific adapter's operating status, such as describing
whether an interface is up, down, or in a testing mode.
<BR>
<BR>
<LI>
SetIpTTL: Allows applications to modify the Time to Live (TTL) field for all IP
packets sent from your system.
<BR>
<BR>
</LI>
</UL>
<H3>
<FONT color="#ff9933">IP Address Class</FONT>
</H3>
This class allows applications to modify and retrieve IP configuration
information. However, most of these APIs are not supported on Windows NT 4.0
Service Pack 4. The IP Helper APIs available for Windows NT 4.0 SP 4 are listed
below:
<UL>
<LI>
GetIpAddrTable: Allows an application to retrieve IP address information.
<BR>
<BR>
</LI>
</UL>
<H3>
<FONT color="#ff9933">ARP Table Class</FONT>
</H3>
This class provides access to the address resolution protocol (ARP) entries by
mapping from an IP address to a physical address. The IP Helper APIs available
for Windows NT 4.0 Service Pack 4 are listed below:
<UL>
<LI>
GetIpNetTable: Retrieves address resolution table information.
<BR>
<BR>
<LI>
SetIpNetEntry: Adds entry to the ARP table.
<BR>
<BR>
<LI>
DeleteIpNetEntry: Deletes entry from the ARP table.
<BR>
<BR>
</LI>
</UL>
<H3>
<FONT color="#ff9933">Route Table Class</FONT>
</H3>
This class allows applications to retrieve IP routing information and permits
applications to make modifications to route entries in the table. Routing is
the selection of the next hop address from a routing table over which a packet
is sent. The IP Helper APIs available for Windows NT 4.0 Service Pack 4 are
listed below:
<UL>
<LI>
GetIpForwardTable: Retrieves routing table information.
<BR>
<BR>
<LI>
SetIpForwardEntry: Adds a route table entry.
<BR>
<BR>
<LI>
DeleteIpForwardEntry: Deletes a route table entry.
<BR>
<BR>
<LI>
GetRTTAndHopCount: Provides approximated round trip time information to a
specific IP destination and also provides the hop count to that destination.
<BR>
<BR>
</LI>
</UL>
<H3>
<FONT color="#ff9933">Statistics Class</FONT>
</H3>
This class provides information on various kinds of packet/connection
statistics information on a local computer. The IP Helper APIs available for
Windows NT 4.0 Service Pack 4 are listed below:
<UL>
<LI>
GetIpStatistics: Retrieves Internet Protocol (IP) packet statistics.
<BR>
<BR>
<LI>
GetIcmpStatistics: Retrieves Internet Control Message Protocol (ICMP)
statistics.
<BR>
<BR>
<LI>
GetTcpStatistics: Retrieves Transmission Control Protocol (TCP) statistics.
<BR>
<BR>
<LI>
GetUdpStatistics: Retrieves User Datagram Protocol (UDP) statistics.
<BR>
<BR>
</LI>
</UL>
<H3>
<FONT color="#ff9933">SOURCE CODE and DEMO APPLICATION</FONT>
</H3>
<P>
I have put together this demo using <B><I>Visual Studio.Net. </I></B>
</P>
<P>
<STRONG><EM>
<TABLE cellSpacing="1" cellPadding="1" width="100%" bgColor="#cccc99" border="1">
<TBODY>
<TR>
<TD>
<FONT color="#008000" size="2">
<P>
// View.cpp : implementation of the CWinNetStatView class
</P>
<P>
//
</P>
<P>
</FONT><FONT color="#0000ff" size="2">#include</FONT><FONT size="2">
"stdafx.h"
</P>
<P>
</FONT><FONT color="#0000ff" size="2">#include</FONT><FONT size="2"> "WinNetStat.h"
</P>
<P>
</FONT><FONT color="#0000ff" size="2">#include</FONT><FONT size="2"> "WinNetStatDoc.h"
</P>
<P>
</FONT><FONT color="#0000ff" size="2">#include</FONT><FONT size="2"> "WinNetStatView.h"
</P>
<P>
</FONT><FONT color="#0000ff" size="2">#include</FONT><FONT size="2"> "netstatmanager.h"
</P>
<P>
</FONT><FONT color="#0000ff" size="2">#ifdef</FONT><FONT size="2"> _DEBUG
</P>
<P>
</FONT><FONT color="#0000ff" size="2">#define</FONT><FONT size="2"> </FONT><FONT color="#0000ff" size="2">
new</FONT><FONT size="2"> DEBUG_NEW
</P>
<P>
</FONT><FONT color="#0000ff" size="2">#endif
</P>
<P>
</FONT><FONT color="#008000" size="2">
</P>
<P>
// CWinNetStatView
</P>
<P>
</FONT><FONT size="2">IMPLEMENT_DYNCREATE(CWinNetStatView, CFormView)
</P>
<P>
BEGIN_MESSAGE_MAP(CWinNetStatView, CFormView)
</P>
<P>
ON_BN_CLICKED(IDC_TCPSTATS, OnClickedTcpstats)
</P>
<P>
ON_BN_CLICKED(IDC_UDPTATS, OnBnClickedUdptats)
</P>
<P>
ON_BN_CLICKED(IDC_IPSTATS, OnBnClickedIpstats)
</P>
<P>
ON_BN_CLICKED(IDC_NW_PARAMS, OnBnClickedNwParams)
</P>
<P>
ON_BN_CLICKED(IDC_ADAPTER_INFO, OnBnClickedAdapterInfo)
</P>
<P>
END_MESSAGE_MAP()
</P>
<P>
</FONT><FONT color="#008000" size="2">
</P>
<P>
// CWinNetStatView construction/destruction
</P>
<P>
</FONT><FONT size="2">CWinNetStatView::CWinNetStatView()
</P>
<P>
: CFormView(CWinNetStatView::IDD)
</P>
<P>
{
</P>
<P>
</FONT><FONT color="#008000" size="2">// TODO: add construction code here
</P>
<P>
</FONT><FONT size="2">}
</P>
<P>
CWinNetStatView::~CWinNetStatView()
</P>
<P>
{
</P>
<P>
}
</P>
<P>
</FONT><FONT color="#0000ff" size="2">void</FONT><FONT size="2"> CWinNetStatView::DoDataExchange(CDataExchange*
pDX)
</P>
<P>
{
</P>
<BLOCKQUOTE dir="ltr" style="MARGIN-RIGHT: 0px">
<P>
CFormView::DoDataExchange(pDX);
</P>
<P>
DDX_Control(pDX, IDC_TEXT_OUTPUT, m_TextOutput);
</P>
</BLOCKQUOTE>
<P>
}
</P>
<P>
BOOL CWinNetStatView::PreCreateWindow(CREATESTRUCT& cs)
</P>
<P>
{
</P>
<BLOCKQUOTE dir="ltr" style="MARGIN-RIGHT: 0px">
<P>
</FONT><FONT color="#008000" size="2">// TODO: Modify the Window class or
styles here by modifying
</P>
<P>
</FONT><FONT color="#008000" size="2">// the CREATESTRUCT cs
</P>
<P>
</FONT><FONT color="#0000ff" size="2">return</FONT><FONT size="2"> CFormView::PreCreateWindow(cs);
</P>
</BLOCKQUOTE>
<P>
}
</P>
<P>
</FONT><FONT color="#0000ff" size="2">
</P>
<P>
void</FONT><FONT size="2"> CWinNetStatView::OnInitialUpdate()
</P>
<P>
{
</P>
<BLOCKQUOTE dir="ltr" style="MARGIN-RIGHT: 0px">
<P>
CFormView::OnInitialUpdate();
</P>
<P>
GetParentFrame()->RecalcLayout();
</P>
<P>
ResizeParentToFit();
</P>
</BLOCKQUOTE>
<P>
}
</P>
<P>
</FONT><FONT color="#008000" size="2">
</P>
<P>
// CWinNetStatView diagnostics
</P>
<P>
</FONT><FONT color="#0000ff" size="2">#ifdef</FONT><FONT size="2"> _DEBUG
</P>
<P>
</FONT><FONT color="#0000ff" size="2">void</FONT><FONT size="2"> CWinNetStatView::AssertValid()
</FONT><FONT color="#0000ff" size="2">const
</P>
<P>
</FONT><FONT size="2">{
</P>
<BLOCKQUOTE dir="ltr" style="MARGIN-RIGHT: 0px">
<P>
CFormView::AssertValid();
</P>
</BLOCKQUOTE>
<P>
}
</P>
<P>
</FONT><FONT color="#0000ff" size="2">
</P>
<P>
void</FONT><FONT size="2"> CWinNetStatView::Dump(CDumpContext& dc) </FONT><FONT color="#0000ff" size="2">
const
</P>
<P>
</FONT><FONT size="2">{
</P>
<BLOCKQUOTE dir="ltr" style="MARGIN-RIGHT: 0px">
<P>
CFormView::Dump(dc);
</P>
</BLOCKQUOTE>
<P>
}
</P>
<P>
CWinNetStatDoc* CWinNetStatView::GetDocument() </FONT><FONT color="#0000ff" size="2">const</FONT><FONT size="2">
</FONT><FONT color="#008000" size="2">// non-debug version is inline
</P>
<P>
</FONT><FONT size="2">{
</P>
<BLOCKQUOTE dir="ltr" style="MARGIN-RIGHT: 0px">
<P>
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CWinNetStatDoc)));
</P>
<P>
</FONT><FONT color="#0000ff" size="2">return</FONT><FONT size="2"> (CWinNetStatDoc*)m_pDocument;
</P>
</BLOCKQUOTE>
<P>
}
</P>
<P>
</FONT><FONT color="#0000ff" size="2">
</P>
<P>
#endif</FONT><FONT size="2"> </FONT><FONT color="#008000" size="2">//_DEBUG
</P>
<P>
// CWinNetStatView message handlers
</P>
<P>
</FONT><FONT color="#0000ff" size="2">void</FONT><FONT size="2"> CWinNetStatView::OnClickedTcpstats()
</P>
<P>
{
</P>
<BLOCKQUOTE dir="ltr" style="MARGIN-RIGHT: 0px">
<P>
</FONT><FONT color="#008000" size="2">//CNetStatManager statMgr; // TODO: Add
your control notification handler code here
</P>
<P>
</FONT><FONT size="2">MIB_TCPSTATS TcpStatsMIB;
</P>
<P>
<FONT color="#0000cc"><STRONG>::GetTcpStatistics(&TcpStatsMIB);</STRONG></FONT>
</P>
<P>
CString strMsg;
</P>
<P>
CString strOutPut;
</P>
<P>
strOutPut.Format("\r\n************************************\r\n");
</P>
<P>
strMsg.Format("\tTCP/IP Statistics\t\r\n");
</P>
<P>
strOutPut+=strMsg;
</P>
<P>
strMsg.Format("\r\n************************************\r\n");
</P>
<P>
strOutPut+=strMsg;
</P>
<P>
strMsg.Format("time-out algorithm:\t%d\r\n",TcpStatsMIB.dwRtoAlgorithm);
</P>
<P>
strOutPut+=strMsg;
</P>
<P>
strMsg.Format("minimum time-out:\t%d\r\n",TcpStatsMIB.dwRtoMin);
</P>
<P>
strOutPut+=strMsg;
</P>
<P>
strMsg.Format("maximum time-out:\t%d\r\n",TcpStatsMIB.dwRtoMax);
</P>
<P>
strOutPut+=strMsg;
</P>
<P>
strMsg.Format("maximum connections:\t%d\r\n",TcpStatsMIB.dwMaxConn);
</P>
<P>
strOutPut+=strMsg;
</P>
<P>
strMsg.Format("active opens:\t%d\r\n",TcpStatsMIB.dwActiveOpens);
</P>
<P>
strOutPut+=strMsg;
</P>
<P>
strMsg.Format("passive opens:\t%d\r\n",TcpStatsMIB.dwPassiveOpens);
</P>
<P>
strOutPut+=strMsg;
</P>
<P>
strMsg.Format("failed attempts:\t%d\r\n",TcpStatsMIB.dwAttemptFails);
</P>
<P>
strOutPut+=strMsg;
</P>
<P>
strMsg.Format("established connections
reset:\t%d\r\n",TcpStatsMIB.dwEstabResets);
</P>
<P>
strOutPut+=strMsg;
</P>
<P>
strMsg.Format("established connections:\t%d\r\n",TcpStatsMIB.dwCurrEstab);
</P>
<P>
strOutPut+=strMsg;
</P>
<P>
strMsg.Format("segments received:\t%d\r\n",TcpStatsMIB.dwInSegs);
</P>
<P>
strOutPut+=strMsg;
</P>
<P>
strMsg.Format("segment sent:\t%d\r\n",TcpStatsMIB.dwOutSegs);
</P>
<P>
strOutPut+=strMsg;
</P>
<P>
strMsg.Format("segments retransmitted:\t%d\r\n",TcpStatsMIB.dwRetransSegs);
</P>
<P>
strOutPut+=strMsg;
</P>
<P>
strMsg.Format("incoming errors:\t%d\r\n",TcpStatsMIB.dwInErrs);
</P>
<P>
strOutPut+=strMsg;
</P>
<P>
strMsg.Format("outgoing resets:\t%d\r\n",TcpStatsMIB.dwOutRsts);
</P>
<P>
strOutPut+=strMsg;
</P>
<P>
strMsg.Format("cumulative connections:\t%d\r\n",TcpStatsMIB.dwNumConns);
</P>
<P>
strOutPut+=strMsg;
</P>
<P>
m_TextOutput.SetWindowText(strOutPut);
</P>
</BLOCKQUOTE>
<P>
}
</P>
<P>
</FONT><FONT color="#0000ff" size="2">
</P>
<P>
void</FONT><FONT size="2"> CWinNetStatView::OnBnClickedUdptats()
</P>
<P>
{
</P>
<BLOCKQUOTE dir="ltr" style="MARGIN-RIGHT: 0px">
<P>
</FONT><FONT color="#008000" size="2">//CNetStatManager statMgr; // TODO: Add
your control notification handler code here
</P>
<P>
</FONT><FONT size="2">MIB_UDPSTATS UDPStatsMIB;
</P>
<P>
<STRONG><FONT color="#0000cc">::GetUdpStatistics(&UDPStatsMIB);</FONT></STRONG>
</P>
<P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -