精华区 [关闭][返回]

当前位置:网易精华区>>讨论区精华>>编程开发>>C/C++>>网络与通讯>>Re:c程序中怎样才能得到主机自身的ip地址哑

主题:Re:c程序中怎样才能得到主机自身的ip地址哑
发信人: szbug(深圳虫)
整理人: wenbobo(2002-12-27 15:47:01), 站内信件
【 在 flybird1026 的大作中提到:】
:
:......
//Win32API:
// WSAStartup
// WSACleanup
// gethostname
// gethostbyname

#include <windows.h>
#include <iostream.h>

void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
 
wVersionRequested = MAKEWORD(2, 2);
 
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 )
{
     /* Tell the user that we could not find a usable */
    /* WinSock DLL.                                  */
     MessageBox(NULL, "Wrong WinSock Version.", "Error", MB_OK);
return;
}

const int iChkIpTimes = 26;
int localIPnums = 0;
char hostName[80];
LPHOSTENT lpHostEnt;
struct in_addr addr[iChkIpTimes];

for ( int i=0; i<iChkIpTimes; i++)
{
memset(&addr[i], 0, sizeof(in_addr));
}

if ( gethostname(hostName, sizeof(hostName)) == SOCKET_ERROR )
{
MessageBox(NULL, "Can't getting local host name.", "Error", MB_OK);
WSACleanup();
return;
}
cout << "Local host name:" << hostName << endl;
lpHostEnt = gethostbyname( hostName );
if ( !lpHostEnt )
{
MessageBox(NULL, "Bad host lookup!", "Error", MB_OK);
WSACleanup();
return;
}
for ( int i=0; i<iChkIpTimes; i++)
{
if ( lpHostEnt->h_addr_list[i] == 0 ) 
{
localIPnums = i;
break;
}
memcpy(&addr[i], lpHostEnt->h_addr_list[i], sizeof(in_addr));
}

for ( int i=0; i<localIPnums; i++)
{
cout << "Local IP Address " << i << " : " << inet_ntoa(addr[i]) << endl;
}

if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 2 )
{
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
WSACleanup();
return;
}
}


----
深圳赛格6546
我是一条虫,来自深圳。

I like rc model. 
免费的动态域名解析  

[关闭][返回]