我现在通过局域网上网
每次上网,我的机器应该有个动态的IP
我用如下函数
void CBetSDlg::myIP(CString& IP)
{
WORD wVersion;
WSADATA wsaData;
char name[255];
PHOSTENT hostinfo;
wVersion=MAKEWORD(2,0);
if(WSAStartup(wVersion,&wsaData)==0)
{
if(gethostname(name,sizeof(name))==0)
{
if((hostinfo=gethostbyname(name))!=NULL)
{
IP=inet_ntoa(*(struct in_addr*)*hostinfo->h_addr_list);
}
}
WSACleanup();
}
}
为什么总是得到局域网中的地质?如何获取动态分配的IP呢?
每次上网,我的机器应该有个动态的IP
我用如下函数
void CBetSDlg::myIP(CString& IP)
{
WORD wVersion;
WSADATA wsaData;
char name[255];
PHOSTENT hostinfo;
wVersion=MAKEWORD(2,0);
if(WSAStartup(wVersion,&wsaData)==0)
{
if(gethostname(name,sizeof(name))==0)
{
if((hostinfo=gethostbyname(name))!=NULL)
{
IP=inet_ntoa(*(struct in_addr*)*hostinfo->h_addr_list);
}
}
WSACleanup();
}
}
为什么总是得到局域网中的地质?如何获取动态分配的IP呢?
解决方案 »
- ATL制作ActiveX时,自绘出问题。
- 使用MFC开发显示网页的程序,如何获得网页的大小
- 我有一个工程,VC编译是都要编译自带的标准库中的源文件,请问是怎么回事?
- 怎么创建只有一个点大小的窗口
- 请大家介绍一下列表控件的用法吧!谢谢!
- 如何引入对话框?
- 请教,我用VC++6.0向导产生了一个MFC应用程序,经过修改后能在主窗口中画图形,请问如何将所画的这些图形一下子全部清除。
- 哪里能找到af_irda.h这样的头文件.
- 谁告诉我怎么上sourceforge啊。好像还是访问不了。
- 关于operator
- 显示位图,急!
- 我学应用物理,计算机该怎么自学?100+100=200 大分相送!不够再加(第2部)
while(hostinfo->next)
{
hostinfo = hostinfo->next;
IP=inet_ntoa(*(struct in_addr*)*hostinfo->h_addr_list);}
char Hostname[100];
HOSTENT *pHostEnt;
int nAdapter = 0; gethostname( Hostname, sizeof( Hostname ));
pHostEnt = gethostbyname( Hostname ); while ( pHostEnt->h_addr_list[nAdapter] )
{
// pHostEnt->h_addr_list[nAdapter] -the current address in host order
nAdapter++;
}
如果共享或者代理上网,你不可能取到动态IP
{
WORD wVersion;
WSADATA wsaData;
char name[255];
PHOSTENT hostinfo;
wVersion=MAKEWORD(2,0);
if(WSAStartup(wVersion,&wsaData)==0)
{
if(gethostname(name,sizeof(name))==0)
{
//IP=inet_ntoa(*(struct in_addr*)*hostinfo->h_addr_list);
int index=0; //罗列本机的所有IP,上网后动态IP是最后一个
while(hostinfo->h_addr_list[index]) //added hostinfo是个连表结构
{
CString s=inet_ntoa(*(struct in_addr*)hostinfo->h_addr_list[index]);
IP.Format("%s/n%s",IP,s);
//AfxMessageBox(IP);
index++;
}
}
WSACleanup();
}
}