//获取公网ip
#include <urlmon.h>
#pragma comment(lib, "urlmon.lib")
#ifdef _UNICODE
#define tString std::wstring
#else
#define tString std::string
#endif
//----------获取客户端公网IP-----------//
tString GetIP()
{
TCHAR szPath[MAX_PATH] = { 0 };
TCHAR szRead[128] = { 0 };
char szSite[] = "http://www.3322.org/dyndns/getip";
GetTempPath(MAX_PATH, szPath);
_tcscat_s(szPath, 256, _T("get_ip.txt"));
if (URLDownloadToFile(0, szSite, szPath, 0, 0) == S_OK)
{
FILE *fIpHtm;
if (fopen_s(&fIpHtm, szPath, "r") == S_OK && fIpHtm)
{
fgets(szRead, 128, fIpHtm);
}
}
return tString(szRead);
}
int _tmain(int argc, _TCHAR* argv[])
{
_tprintf(_T("\nget net ip!\n"));
_tprintf(_T("%s\n"), GetIP().c_str());
getchar();
return 0;
}