网络
1.01 获得本地计算机主机名称/ip信息
{
//初始化WinSock
WSADATA WSAData;
if (WSAStartup(MAKEWORD(2, 0), &WSAData) != 0)
{
return;
}
//获得本地计算机主机名称
char hostName[1024] = { 0 };
gethostname(hostName, sizeof(hostName));
//获得主机信息
struct hostent* pHostEnt = gethostbyname(hostName);
if (pHostEnt == NULL)
{
return;
}
//获得主机IP地址
int n = 0;
while (pHostEnt->h_addr_list[n] != NULL)
{
CString strTemp = _T("");
strTemp.Format(_T("%d.%d.%d.%d\n"),
(pHostEnt->h_addr_list[n][0] & 0x00FF),
(pHostEnt->h_addr_list[n][1] & 0x00FF),
(pHostEnt->h_addr_list[n][2] & 0x00FF),
(pHostEnt->h_addr_list[n][3] & 0x00FF));
n++;
}
//清理WinSock
WSACleanup();
}
1.02 通过IP地址获得主机名称
{
//初始化WinSock
WSADATA WSAData;
if (WSAStartup(MAKEWORD(2, 0), &WSAData) != 0)
{
return;
}
//获得IP地址
UpdateData(TRUE);
if (m_ctrlAddr.IsBlank())
{
AfxMessageBox(_T("IP地址不能为空。"));
return;
}
BYTE Addr[4];
m_ctrlAddr.GetAddress(Addr[0], Addr[1], Addr[2], Addr[3]);
//获得主机信息
struct hostent* pHostEnt = gethostbyaddr((char*)Addr, 4, AF_INET);
if (pHostEnt == NULL)
{
return;
}
//获得主机名称
CString strText = _T("");
strText.Format(_T("%d.%d.%d.%d的主机名称:%s"),
Addr[0],
Addr[1],
Addr[2],
Addr[3],
pHostEnt->h_name);
AfxMessageBox(strText);
//清理WinSock
WSACleanup();
}
1.03 获得本地计算机的MAC地址
添加头文件
#include <Nb30.h>
链接-》附加依赖项
Iphlpapi.lib
netapi32.lib
void CDemoDlg::OnTest()
{
LANA_ENUM num;
ADAPTER_STATUS status;
NCB ncb;
//发送NCBENUM命令,获得网卡的数量和每个网卡的编号
ncb.ncb_command = NCBENUM;
ncb.ncb_buffer = (UCHAR*)#
ncb.ncb_length = sizeof(LANA_ENUM);
Netbios(&ncb);
CString strText = _T("本地计算机的MAC地址:\n");
for (int n = 0; n < num.length; n++)
{
//发送NCBRESET命令
memset(&ncb, 0, sizeof(NCB));
ncb.ncb_command = NCBRESET;
ncb.ncb_lana_num = num.lana[n];
Netbios(&ncb);
//发送NCBASTAT命令,获得每个网卡的状态
memset(&ncb, 0, sizeof(NCB));
ncb.ncb_command = NCBASTAT;
ncb.ncb_lana_num = num.lana[n];
memset(ncb.ncb_callname, '*', 1);
ncb.ncb_buffer = (UCHAR*)&status;
ncb.ncb_length = sizeof(ADAPTER_STATUS);
Netbios(&ncb);
//获得每个网卡的MAC地址
CString strTemp = _T("");
strTemp.Format(L"%02X-%02X-%02X-%02X-%02X-%02X\n",
status.adapter_address[0],
status.adapter_address[1],
status.adapter_address[2],
status.adapter_address[3],
status.adapter_address[4],
status.adapter_address[5]);
strText += strTemp;
}
AfxMessageBox(strText);
}
1.04 获得本地计算机的子网掩码
void CDemoDlg::OnTest()
{
//获得需要的缓冲区大小
DWORD nLength = 0;
if (GetAdaptersInfo(NULL, &nLength) != ERROR_BUFFER_OVERFLOW)
{
return;
}
IP_ADAPTER_INFO* pAdapterInfo = (IP_ADAPTER_INFO*)new BYTE[nLength];
//获得本地计算机网卡信息
if (GetAdaptersInfo(pAdapterInfo, &nLength) != ERROR_SUCCESS)
{
delete[] pAdapterInfo;
return;
}
//获得本地计算机子网掩码
CString strText =_T("本地计算机的子网掩码:\n");
IP_ADAPTER_INFO* pCurrentAdapterInfo = pAdapterInfo;
while (pCurrentAdapterInfo != NULL)
{
CString strTemp = _T("");
strTemp.Format(_T("%s\n"),
pCurrentAdapterInfo->IpAddressList.IpMask.String);
strText += strTemp;
pCurrentAdapterInfo = pCurrentAdapterInfo->Next;
}
AfxMessageBox(strText);
delete[] pAdapterInfo;
}
1.05 获得本地计算机的域名
void CDemoDlg::OnTest()
{
//获得需要的缓冲区大小
DWORD nLength = 0;
if (GetNetworkParams(NULL, &nLength) != ERROR_BUFFER_OVERFLOW)
{
return;
}
FIXED_INFO* pFixedInfo = (FIXED_INFO*)new BYTE[nLength];
//获得本地计算机网络参数
if (GetNetworkParams(pFixedInfo, &nLength) != ERROR_SUCCESS)
{
delete[] pFixedInfo;
return;
}
//获得本地计算机域名
CString strText = _T("");
strText.Format(_T("本地计算机的域名:\n%s"), pFixedInfo->DomainName);
AfxMessageBox(strText);
delete[] pFixedInfo;
}
1.06 获得本地计算机的DNS服务器地址
void CDemoDlg::OnTest()
{
//获得需要的缓冲区大小
DWORD nLength = 0;
if (GetNetworkParams(NULL, &nLength) != ERROR_BUFFER_OVERFLOW)
{
return;
}
FIXED_INFO* pFixedInfo = (FIXED_INFO*)new BYTE[nLength];
//获得本地计算机网络参数
if (GetNetworkParams(pFixedInfo, &nLength) != ERROR_SUCCESS)
{
delete[] pFixedInfo;
return;
}
//获得本地计算机DNS服务器地址
CString strText = _T("本地计算机的DNS地址:\n");
IP_ADDR_STRING* pCurrentDnsServer = &pFixedInfo->DnsServerList;
while (pCurrentDnsServer != NULL)
{
CString strTemp = _T("");
strTemp.Format(_T("%s\n"), pCurrentDnsServer->IpAddress.String);
strText += strTemp;
pCurrentDnsServer = pCurrentDnsServer->Next;
}
AfxMessageBox(strText);
delete[] pFixedInfo;
}
1.07 获得本地计算机的网络协议信息
void CDemoDlg::OnTest()
{
//初始化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();
}
1.08 获得本地计算机的TCP协议统计信息
void CDemoDlg::OnTest()
{
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);
}
1.09 获得本地计算机的IP协议统计信息
void CDemoDlg::OnTest()
{
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);
}
1.10 获得本地计算机的UDP协议统计信息
void CDemoDlg::OnTest()
{
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);
}
1.11 获得网上邻居
void CDemoDlg::EnumNet(NETRESOURCE* pResource, int nItem)
{
HANDLE hEnum = NULL;
DWORD dwResult = 0;
//打开网上资源的枚举
dwResult = WNetOpenEnum(
RESOURCE_GLOBALNET,
RESOURCETYPE_ANY,
0,
pResource,
&hEnum);
if (dwResult != NO_ERROR)
{
return;
}
DWORD nCount = -1;
DWORD nSize = 16384;
pResource = (NETRESOURCE*)new BYTE[nSize];
do
{
ZeroMemory(pResource, nSize);
//枚举网上资源
dwResult = WNetEnumResource(
hEnum,
&nCount,
pResource,
&nSize);
if (dwResult == NO_ERROR)
{
for (UINT n = 0 ; n < nCount; n++)
{
ShowNet(&pResource[n], nItem);
//如果网络资源是容器资源则递归调用枚举函数
if ((pResource[n].dwUsage & RESOURCEUSAGE_CONTAINER)
== RESOURCEUSAGE_CONTAINER)
{
EnumNet(&pResource[n], nItem + 1);
}
}
}
else if(dwResult != ERROR_NO_MORE_ITEMS)
{
break;
}
} while(dwResult != ERROR_NO_MORE_ITEMS);
delete[] pResource;
//关闭网上资源的枚举
WNetCloseEnum(hEnum);
}
void CDemoDlg::ShowNet(NETRESOURCE* pResource, int nItem)
{
CString strText = _T("");
strText += _T("");
strText += pResource->lpRemoteName;
strText += _T("");
strText += pResource->lpLocalName;
strText += _T("");
strText += pResource->lpComment;
strText += _T("");
for (int n = 0; n < nItem; n++)
{
strText = _T(" ") + strText;
}
CListBox* pListBox = (CListBox*)GetDlgItem(IDC_LIST);
pListBox->AddString(strText);
}
1.12 实现网络浏览器
创建CDemoView 类继承0 CHtmlView类
void CDemoView::OnGoBack()
{
//后退
GoBack();
}
void CDemoView::OnGoForward()
{
//前进
GoForward();
}
void CDemoView::OnStop()
{
//停止
Stop();
}
void CDemoView::OnGoRefresh()
{
//刷新
Refresh();
}
void CDemoView::OnGoHome()
{
//主页
GoHome();
}
void CDemoView::OnGoSerach()
{
//搜索
GoSearch();
}
void CDemoView::OnEnterAddress()
{
//获得网址
CMainFrame* pMainFrame = (CMainFrame*)AfxGetApp()->m_pMainWnd;
CComboBoxEx* pAddressBar = &pMainFrame->m_wndAddressBar;
CString strURL = _T("");
pAddressBar->GetWindowText(strURL);
//浏览网页
Navigate2(strURL, 0, NULL);
//添加地址列表
COMBOBOXEXITEM item;
item.mask = CBEIF_TEXT;
item.iItem = -1;
item.pszText = (LPTSTR)(LPCTSTR)strURL;
pAddressBar->InsertItem(&item);
}