VC++之网络通信技术之获取局域网其它计算机IP地址.docVIP

  • 5
  • 0
  • 约3.79千字
  • 约 6页
  • 2018-06-07 发布于未知
  • 举报

VC++之网络通信技术之获取局域网其它计算机IP地址.doc

VC++之网络通信技术之获取局域网其它计算机的IP地址 2010年11月22日 星期一 21:59 一、创建对话框应用程序 二、编辑对话框资源 控件ID及标题 IDC_LIST_IP IDC_GETOTHERIP????????????????????????????????????获取局域网其它计算机的IP地址(属性有“浮动”) 三、添加变量、函数 ??????? 1、添加变量 ??????? 2、添加消息响应函数 四、添加代码 ????????? 1、于“GetOtherIPDlg.cpp”文件内添加包含语句 #include stdafx.h #include GetOtherIP.h #include GetOtherIPDlg.h #include Winsock2.h ????????? 2、于“GetOtherIPDlg.cpp”文件内添加消息响应函数代码 void CGetOtherIPDlg::OnGetotherip() { // TODO: Add your control notification handler code here struct hostent *host; struct in_addr *ptr; // 检索IP地址 DWORD dwScope = RESOURCE_CONTEXT; NETRESOURCE *NetResource = NULL; HANDLE hEnum; WNetOpenEnum( dwScope, NULL, NULL, NULL, hEnum ); WSADATA wsaData; WSAStartup(MAKEWORD(1,1),wsaData); CString strTemp; if ( hEnum ) { ?? DWORD Count = 0xFFFFFFFF; ?? DWORD BufferSize = 2048; ?? LPVOID Buffer = new char[2048]; ?? WNetEnumResource( hEnum, Count, Buffer, BufferSize ); ?? NetResource = (NETRESOURCE*)Buffer; ?? strTemp.Format(局域网内的计算机名称及其IP地址为:); ?? m_ListIP.AddString(strTemp); ?? char szHostName[200]; ?? unsigned int i; ?? for ( i = 0; i BufferSize/sizeof(NETRESOURCE); i++, NetResource++ ) ?? { ??? if ( NetResource-dwUsage == RESOURCEUSAGE_CONTAINER ???? NetResource-dwType == RESOURCETYPE_ANY ) ??? { ???? if ( NetResource-lpRemoteName ) ???? { ????? CString strFullName = NetResource-lpRemoteName; ????? if ( 0 == strFullName.Left(2).Compare(\\\\) )??? ?????? strFullName = strFullName.Right(strFullName.GetLength()-2); ????? //gethostname( szHostName, strlen( szHostName ) ); ????? host = gethostbyname(strFullName); ????? if(host == NULL) continue; ????? ptr = (struct in_addr *)host-h_addr_list[0];???????????????????? ????? // =. 分隔开IP:6.????????????? ????? int a = ptr-S_un.S_un_b.s_b1; // 211??????????? ????? int b = ptr-S_un.S_un_b.s_b2; // 40 ????? int c = ptr-S_un.S_un_b.s_b3; // 35 ????? int d = ptr-S_un.S_un_b.s_b4; // 76 ????? strTemp.Format( 计算机%d名称:%s--IP地址:%d.%d.%d.%d\r\n, i,strFullName,a,b,c,d); ?

文档评论(0)

1亿VIP精品文档

相关文档