| 论坛登陆 注册 | 教程 笑话 影视 投稿 |
![]() |
|
||||||||||||||||||||||||||||||||||||||||
用VC获取本机IP地址列表的方法 |
|
| www.xker.com 作者: 来源:csdn 加入日期:2006-2-26 10:58:50 | |
// // 程序功能:取出本机的所有IP,保存为一个字符串列表 // 作者:someone(阿风) // windows xp + sp2 , vc6 + sp5编译通过 // #include using std::cin; using std::cout; using std::cerr; using std::endl; #include using std::list; #include using std::string; #include #pragma comment( lib, "ws2_32.lib" ) bool GetIpList( list { WORD wVersionRequested; WSADATA wsaData; int err; wVersionRequested = MAKEWORD( 2, 2 ); err = WSAStartup( wVersionRequested, &wsaData ); if ( err != 0 ) { cout << "WSAStartup failed !" << endl; return false; } char szhn[256]; int nStatus = gethostname(szhn, sizeof(szhn)); if (nStatus == SOCKET_ERROR ) { cout << "gethostname failed, Error code: " << WSAGetLastError() << endl; return false; } HOSTENT *host = gethostbyname(szhn); if (host != NULL) { for ( int i=0; ; i++ ) { r_iplist.push_back( inet_ntoa( *(IN_ADDR*)host->h_addr_list[i] ) ) ; if ( host->h_addr_list[i] + host->h_length >= host->h_name ) break; } } WSACleanup(); return true; } int main(int argc, char *argv[]) { list std::list if ( !GetIpList( iplist ) ) { cout << "Get ip list failed !" << endl; return -1; } cout << "IP list :" << endl; for ( ip = iplist.begin(); ip != iplist.end(); ip++ ) { cout << " " << *ip << endl; } return 0; } 编辑:xker.com 上一篇:简单的摄像头视频浏览和捕获程序 下一篇:没有了 |
||
| 【关闭窗口】【浏览次数:】【收藏此页】 |
|
| 评论 | |
设为首页 - 版权声明 - 广告服务 - 关于我们 - 联系我们 - 友情连接 |
| Copyright © 2003-2006 xker.com All rights reserved. 网站合作、广告联系QQ:12231446 |
| 小新技术网 冀ICP备05002857号 |