闪电般的获取外网ip地址和内网ip
闪电般的获取外网ip地址和内网ip
//获取本机内网IP
/*hostent *name;
char Name[255];
memset(Name,0,255);
gethostname(Name,255);
name=gethostbyname(Name);
m_ClientIP=inet_ntoa(*(in_addr *)name->h_addr_list[0]);*/
//获取本机公网IP地址
CString strClientIP;
int iFirst = 0, iLast = 0;
CString strIPAddr;
TCHAR szIPAddr[MAX_PATH] = {0};
TCHAR szTempFile [MAX_PATH] = {0};
UINT nResult= GetTempFileName (szTempFile, _T ("~ex"),0,szTempFile);
int ret = URLDownloadToFile(NULL,", BINDF_GETNEWESTVERSION, NULL);
if (ret == S_OK) {
CFile mFile;
if (mFile.Open(szTempFile, CFile::modeRead) && mFile.GetLength() > 0){
mFile.Read(szIPAddr, MAX_PATH);
strIPAddr = szIPAddr;
iFirst = strIPAddr.Find('[', 0)+1;
iLast = strIPAddr.Find(']', iFirst);
strIPAddr = strIPAddr.Mid(iFirst, iLast-iFirst);
strClientIP=strIPAddr;
}
}
评论已关闭