获得TCP,IP,UDP协议信息

这段代码用于获取本地计算机上的TCP,IP,UDP协议的详细统计信息。通过调用Windows Socket API,分别展示了TCP的连接状态、重传次数等,IP的转发设置、接收错误等,以及UDP的接收和发送数据报情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//初始化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);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值