参考了部分网友的资源,改写了一个获取公网IP地址的方法。忘记了网友网址,如冒犯,请原谅!发布此代码的目的旨在交流。代码如下: //头文件 #include "afxinet.h" //获取公网IP地址 CString getLocalIpAddress() { CInternetSession session; //声明该对象为获取网页属性做准备 CHttpFile *pFile=NULL; CString str,ch; CString m_szSite="http://www.zu14.cn/ip/"; //该地址是获取外网IP的关 键,原理就是通过该地址来获取外网IP的 try{ pFile=(CHttpFile*)session.OpenURL(m_szSite);} //打开该地 址 catch(CInternetException *pEx) //错误处理 { pFile=NULL; pEx->Delete(); } if(pFile) { while(pFile->ReadString(str)) //读入该字符串 { ch+=str+"\r\n"; } pFile->Close(); delete pFile; } else { ch+=""; } return ch;//返回公网IP地址 } CString jian(CString ch) { CString aaa; for(int i=0;i<ch.GetLength();i++) { if(ch.Mid(i,1)=='0'||ch.Mid(i,1)=='1'||ch.Mid(i,1) =='2'||ch.Mid(i,1)=='3'||ch.Mid(i,1)=='4'||ch.Mid(i,1)=='5'||ch.Mid(i,1) =='6'||ch.Mid(i,1)=='7'||ch.Mid(i,1)=='8'||ch.Mid(i,1)=='9'||ch.Mid(i,1)=='.') aaa+=ch.Mid(i,1); else break; } return(aaa); }
#include "afxinet.h"
//获取公网IP地址
CString getLocalIpAddress()
{
CInternetSession session; //声明该对象为获取网页属性做准备
CHttpFile *pFile=NULL;
CString str,ch;
CString m_szSite="http://www.zu14.cn/ip/"; //该地址是获取外网IP的关键,原理就是通过该地址来获取外网IP的
try{
pFile=(CHttpFile*)session.OpenURL(m_szSite);} //打开该地址
catch(CInternetException *pEx) //错误处理
{
pFile=NULL;
pEx->Delete();
}
if(pFile)
{
while(pFile->ReadString(str)) //读入该字符串
{
ch+=str+"\r\n";
}
pFile->Close();
delete pFile;
}
else
{
ch+="";
- jxw_00172013-11-16没有测试通过
- hili2102013-06-30貌似是通过网站获取的方式,最近下到了一个非网页获取方式,挺不错的,打算用来作为服务器变更IP的通知软件
- beizhongshui_71002013-08-10通过ip138获取到了,试试这种办法。能不能vc直接读ip管理服务器或解析IP段。而不依赖于具体的网站。
- 粉丝: 43
- 资源: 11
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助