获得TCP,IP,UDP协议信息

来源:互联网 发布:淘宝里的评价怎么删除 编辑:程序博客网 时间:2024/06/06 14:13
//初始化WinSock
WSADATA WSAData;
if (WSAStartup(MAKEWORD(2,0), &WSAData)!= 0)
{
return;
}


int nResult = 0;


//获得需要的缓冲区大小
DWORD nLength = 0;
nResult = WSAEnumProtocols(NULL, NULL, &nLength);
if (nResult != SOCKET_ERROR)
{
return;
}
if (WSAGetLastError() != WSAENOBUFS)
{
return;
}


WSAPROTOCOL_INFO* pProtocolInfo = (WSAPROTOCOL_INFO*)new BYTE[nLength];


//获得本地计算机协议信息
nResult = WSAEnumProtocols(NULL, pProtocolInfo, &nLength);
if (nResult == SOCKET_ERROR)
{
delete[] pProtocolInfo;
return;
}
for (int n = 0; n < nResult; n++)
{
m_ctrlList.AddString(pProtocolInfo[n].szProtocol);
}


delete[] pProtocolInfo;


//清理WinSock
WSACleanup();
CListBox* pListBox = (CListBox*)GetDlgItem(IDC_LIST);
pListBox->ResetContent();


MIB_TCPSTATS TCPStats;


//获得TCP协议统计信息
if (GetTcpStatistics(&TCPStats) != NO_ERROR)
{
return;
}


CString strText = _T("");
strText.Format(_T("time-out algorithm:%d"), 
TCPStats.dwRtoAlgorithm);
pListBox->AddString(strText);
strText.Format(_T("minimum time-out:%d"), 
TCPStats.dwRtoMin);
pListBox->AddString(strText);
strText.Format(_T("maximum time-out:%d"), 
TCPStats.dwRtoMax);
pListBox->AddString(strText);
strText.Format(_T("maximum connections:%d"), 
TCPStats.dwMaxConn);
pListBox->AddString(strText);
strText.Format(_T("active opens:%d"), 
TCPStats.dwActiveOpens);
pListBox->AddString(strText);
strText.Format(_T("passive opens:%d"), 
TCPStats.dwPassiveOpens);
pListBox->AddString(strText);
strText.Format(_T("failed attempts:%d"), 
TCPStats.dwAttemptFails);
pListBox->AddString(strText);
strText.Format(_T("established connections reset:%d"), 
TCPStats.dwEstabResets);
pListBox->AddString(strText);
strText.Format(_T("established connections:%d"), 
TCPStats.dwCurrEstab);
pListBox->AddString(strText);
strText.Format(_T("segments received:%d"), 
TCPStats.dwInSegs);
pListBox->AddString(strText);
strText.Format(_T("segment sent:%d"), 
TCPStats.dwOutSegs);
pListBox->AddString(strText);
strText.Format(_T("segments retransmitted:%d"), 
TCPStats.dwRetransSegs);
pListBox->AddString(strText);
strText.Format(_T("incoming errors:%d"), 
TCPStats.dwInErrs);
pListBox->AddString(strText);
strText.Format(_T("outgoing resets:%d"), 
TCPStats.dwOutRsts);
pListBox->AddString(strText);
strText.Format(_T("cumulative connections:%d"), 
TCPStats.dwNumConns);
pListBox->AddString(strText);


CListBox* pListBox = (CListBox*)GetDlgItem(IDC_LIST);
pListBox->ResetContent();


MIB_IPSTATS IPStats;


//获得IP协议统计信息
if (GetIpStatistics(&IPStats) != NO_ERROR)
{
return;
}


CString strText = _T("");
strText.Format(_T("IP forwarding enabled or disabled:%d"), 
IPStats.dwForwarding);
pListBox->AddString(strText);
strText.Format(_T("default time-to-live:%d"), 
IPStats.dwDefaultTTL);
pListBox->AddString(strText);
strText.Format(_T("datagrams received:%d"), 
IPStats.dwInReceives);
pListBox->AddString(strText);
strText.Format(_T("received header errors:%d"), 
IPStats.dwInHdrErrors);
pListBox->AddString(strText);
strText.Format(_T("received address errors:%d"), 
IPStats.dwInAddrErrors);
pListBox->AddString(strText);
strText.Format(_T("datagrams forwarded:%d"), 
IPStats.dwForwDatagrams);
pListBox->AddString(strText);
strText.Format(_T("datagrams with unknown protocol:%d"), 
IPStats.dwInUnknownProtos);
pListBox->AddString(strText);
strText.Format(_T("received datagrams discarded:%d"), 
IPStats.dwInDiscards);
pListBox->AddString(strText);
strText.Format(_T("received datagrams delivered:%d"), 
IPStats.dwInDelivers);
pListBox->AddString(strText);
strText.Format(_T("outgoing datagrams requested to send:%d"), 
IPStats.dwOutRequests);
pListBox->AddString(strText);
strText.Format(_T("outgoing datagrams discarded:%d"), 
IPStats.dwOutDiscards);
pListBox->AddString(strText);
strText.Format(_T("sent datagrams discarded:%d"), 
IPStats.dwOutDiscards);
pListBox->AddString(strText);
strText.Format(_T("datagrams for which no route exists:%d"), 
IPStats.dwOutNoRoutes);
pListBox->AddString(strText);
strText.Format(_T("datagrams for which all frags did not arrive:%d"), 
IPStats.dwReasmTimeout);
pListBox->AddString(strText);
strText.Format(_T("datagrams requiring reassembly:%d"), 
IPStats.dwReasmReqds);
pListBox->AddString(strText);
strText.Format(_T("successful reassemblies:%d"), 
IPStats.dwReasmOks);
pListBox->AddString(strText);
strText.Format(_T("failed reassemblies:%d"), 
IPStats.dwReasmFails);
pListBox->AddString(strText);
strText.Format(_T("successful fragmentations:%d"), 
IPStats.dwFragOks);
pListBox->AddString(strText);
strText.Format(_T("failed fragmentations:%d"), 
IPStats.dwFragFails);
pListBox->AddString(strText);
strText.Format(_T("datagrams fragmented:%d"), 
IPStats.dwFragCreates);
pListBox->AddString(strText);
strText.Format(_T("number of interfaces on computer:%d"), 
IPStats.dwNumIf);
pListBox->AddString(strText);
strText.Format(_T("number of IP address on computer:%d"), 
IPStats.dwNumAddr);
pListBox->AddString(strText);
strText.Format(_T("number of routes in routing table:%d"), 
IPStats.dwNumRoutes);
pListBox->AddString(strText);


CListBox* pListBox = (CListBox*)GetDlgItem(IDC_LIST);
pListBox->ResetContent();


MIB_UDPSTATS UDPStats;


//获得UDP协议统计信息
if (GetUdpStatistics(&UDPStats) != NO_ERROR)
{
return;
}


CString strText = _T("");
strText.Format(_T("received datagrams:%d\t\n"), 
UDPStats.dwInDatagrams);
pListBox->AddString(strText);
strText.Format(_T("datagrams for which no port exists:%d\t\n"), 
UDPStats.dwNoPorts);
pListBox->AddString(strText);
strText.Format(_T("errors on received datagrams:%d\t\n"), 
UDPStats.dwInErrors);
pListBox->AddString(strText);
strText.Format(_T("sent datagrams:%d\t\n"), 
UDPStats.dwOutDatagrams);
pListBox->AddString(strText);
strText.Format(_T("number of entries in UDP listener table:%d\t\n"), 
UDPStats.dwNumAddrs);
pListBox->AddString(strText);