获取本机ip和外网ip地址
来源:互联网 发布:订演唱会门票软件 编辑:程序博客网 时间:2024/04/30 18:57
//获取外网地址
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;
}
0 0
- 获取本机ip和外网ip地址
- 获取本机IP地址
- 获取本机IP地址
- 获取本机IP地址
- 获取本机IP地址
- 获取本机IP地址
- 获取本机IP地址
- 获取本机IP地址
- 获取本机IP地址
- 获取本机IP地址
- 获取本机ip地址
- 获取本机IP地址
- 获取本机ip地址
- 获取本机ip地址
- 获取本机IP地址
- 获取本机IP地址
- 获取 本机 ip 地址
- 获取本机IP地址
- 第二周~
- Netty4.0学习笔记系列之四:混合使用coder和handler
- 利用VS2012对文本文件的单词频率统计,并分析结果
- 我的android(第十一天)
- function overload 2
- 获取本机ip和外网ip地址
- [android] viewpager获取当前view报空指针的解决方法
- (中国剩余定理) hdu 1573 X问题
- Oracle LISTENER 主机名修改为IP地址后LISTENER无法监听到实例 oracle监听错误与hosts文件配置
- 使用nginx搭建媒体点播服务器
- 设计模式学习(二) 简单工厂模式
- 快鱼吃慢鱼,基于汉澳sinox的ports软件移植在行动
- 第三周作业
- 交换两个变量的值