//获取外网地址
void GetNetIPAddress(__out std::string& IpAddr)
{
static std::string LocalIpAddr;
if(LocalIpAddr.empty())
{
char pName[256] = {0};
gethostname(pName,sizeof(pName));
hostent *pHost = gethostbyname(pName);
string str;
HRESULT hr = URLDownloadToFile(0,_T("http://iframe.ip138.com/ic.asp"),_T("ip.ini"),0,NULL);
if(S_OK == hr)
{
FILE *pFile = fopen("ip.ini","r");
LONG curpos = ftell(pFile);
fseek(pFile,0,SEEK_END);
LONG length = ftell(pFile);
fseek(pFile,curpos,SEEK_SET);
char *pData = new char[length+1];
fread(pData,length,1,pFile);
fclose(pFile);
str = pData;
int nIndex1 = str.find('[');
int nIndex2 = str.find(']');
LocalIpAddr = str.substr(nIndex1+1,nIndex2-nIndex1-1);
}
else
{
LocalIpAddr = inet_ntoa(*((in_addr*)pHost->h_addr));
}
}
IpAddr = LocalIpAddr;
}
//获取本机地址
int GetLocalIPAddress(__out std::string& IpAddr)
{
char host_name[255] = "0";
//获取本地主机名称
if(gethostname(host_name, sizeof(host_name)) == SOCKET_ERROR)
{
return 1;
}
//从主机名数据库中得到对应的“主机”
struct hostent *phe = gethostbyname(host_name);
if (phe == 0)
{
return 1;
}
//循环得出本地机器所有IP地址
for(int i = 0; phe->h_addr_list[i] != 0; ++i)
{
struct in_addr addr;
memcpy(&addr, phe->h_addr_list[i], sizeof(struct in_addr));
IpAddr = inet_ntoa(addr);
return 0;
}
return 1;
}
void GetNetIPAddress(__out std::string& IpAddr)
{
static std::string LocalIpAddr;
if(LocalIpAddr.empty())
{
char pName[256] = {0};
gethostname(pName,sizeof(pName));
hostent *pHost = gethostbyname(pName);
string str;
HRESULT hr = URLDownloadToFile(0,_T("http://iframe.ip138.com/ic.asp"),_T("ip.ini"),0,NULL);
if(S_OK == hr)
{
FILE *pFile = fopen("ip.ini","r");
LONG curpos = ftell(pFile);
fseek(pFile,0,SEEK_END);
LONG length = ftell(pFile);
fseek(pFile,curpos,SEEK_SET);
char *pData = new char[length+1];
fread(pData,length,1,pFile);
fclose(pFile);
str = pData;
int nIndex1 = str.find('[');
int nIndex2 = str.find(']');
LocalIpAddr = str.substr(nIndex1+1,nIndex2-nIndex1-1);
}
else
{
LocalIpAddr = inet_ntoa(*((in_addr*)pHost->h_addr));
}
}
IpAddr = LocalIpAddr;
}
//获取本机地址
int GetLocalIPAddress(__out std::string& IpAddr)
{
char host_name[255] = "0";
//获取本地主机名称
if(gethostname(host_name, sizeof(host_name)) == SOCKET_ERROR)
{
return 1;
}
//从主机名数据库中得到对应的“主机”
struct hostent *phe = gethostbyname(host_name);
if (phe == 0)
{
return 1;
}
//循环得出本地机器所有IP地址
for(int i = 0; phe->h_addr_list[i] != 0; ++i)
{
struct in_addr addr;
memcpy(&addr, phe->h_addr_list[i], sizeof(struct in_addr));
IpAddr = inet_ntoa(addr);
return 0;
}
return 1;
}