如何能够获得本机的IP地址
编号:QA001132
建立日期: 1999年6月8日 最后修改日期:1999年6月8日
所属类别:
xtay:
vc++50
win95/98
如何能够获得本机的IP地址?
回答:
采用下面的代码可以获得本机IP地址:
char szHostName[128];
if( gethostname(szHostName, 128) == 0 )
{
// Get host adresses
struct hostent * pHost;
int i;
pHost = gethostbyname(szHostName);
for( i = 0; pHost!= NULL && pHost->h_addr_list[i]!= NULL; i++ )
{
CString str;
int j;
for( j = 0; j < pHost->h_length; j++ )
{
CString addr;
if( j > 0 )
str += ".";
addr.Format("%u", (unsigned int)((unsigned
char*)pHost->h_addr_list[i])[j]);
str += addr;
}
// str now contains one local IP address - do whatever you want to do with it (probably add it to a list)
}
}
此问题由李海回答。
| |
|
|
| |
|
|