如何得到形如xxx.xxx.xxx.xxx字串形式的本机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)
}
}
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)
}
}
解决方案 »
- 请教一个关于GDI+ CPU占用过高的问题?
- 关于如何在本程序中屏蔽鼠标的问题,求解答
- 空间几何中点(x1,y1,z1)在空间线段的左边还是右边?
- 自己编写的程序,本来可以运行,但运用了动态链接库后,程序运行时不能启动界面,只能看到进程,这是为什么?
- 批量转换rich edit内字符颜色的问题
- 平台SDK的函数, MFC 类内部成员函数, Win32 API 函数 有什么区别?
- 用自动化“挂羊头卖狗肉”?
- 新手问题:怎样知道 VC 与 SQL 是用什么方法连接的?
- 请问如何像winamp,金山毒霸一样修改标题栏,最大化、最小化按钮?有例程最好,[email protected]
- 超酷YSChat3.0网络视频会议源码
- 看看我们的软件,技术上如何?
- 关于写项目开发阶段报告的问题?
int gethostname (
char FAR * name,
int namelen
);
然后根据本机名取得IP地址
struct hostent FAR * gethostbyname (
const char FAR * name
);