得到计算机的主机名和IP地址 (转)[@more@]
得到计算机的主机名和IP地址 作者:
转自: 中华技术网
编译:刘峰
环境:Visual C++ 5 SP1/2 WINDOW NT4或 windows 95.
需求:#include
链接库:Wsock32.lib
详细代码:
{
word wVersionRequested;
WSADATA wsaData;
char name[255];
CString ip;
PHOSTENT hostinfo;
wVersionRequested = MAKEWORD( 2, 0 );
if ( W SAStartup( wVersionRequested, &wsaData ) == 0 )
{
if( gethostname ( name, sizeof(name)) == 0)
{
if((hostinfo = gethostbyname(name)) != NULL)
{
ip = .NET_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
}
}
WSACleanup( );
}
}
转自: 中华技术网
编译:刘峰
环境:Visual C++ 5 SP1/2 WINDOW NT4或 windows 95.
需求:#include
链接库:Wsock32.lib
详细代码:
{
word wVersionRequested;
WSADATA wsaData;
char name[255];
CString ip;
PHOSTENT hostinfo;
wVersionRequested = MAKEWORD( 2, 0 );
if ( W SAStartup( wVersionRequested, &wsaData ) == 0 )
{
if( gethostname ( name, sizeof(name)) == 0)
{
if((hostinfo = gethostbyname(name)) != NULL)
{
ip = .NET_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
}
}
WSACleanup( );
}
}
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10752043/viewspace-988383/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/10752043/viewspace-988383/
本文介绍了一种使用Visual C++ 5 SP1/2在Windows NT4或Windows 95环境下获取计算机主机名和IP地址的方法。通过提供的示例代码,展示了如何利用WSAStartup初始化Winsock库,并通过gethostname和gethostbyname函数实现主机名和IP地址的获取。

被折叠的 条评论
为什么被折叠?



