获取本机内网、外网ip(C++)

  • 基础知识

  1.  电脑在局域网内,通过网关/路由器连接到Internet则ip分为内网ip、外网ip。通过ipconfig得到的为局域网ip。
  2. 电脑直接拨号连接等,则本机通过ipconfig得到的就是外网ip。

  • 代码
  • //Get IP
    int GetLocalIP(std::string &local_ip);
    int GetInternetIP(std::string &Inernet_ip);
  • int GetLocalIP( std::string &local_ip )
    {
    	WSADATA wsaData = {0};
    	if (WSAStartup(MAKEWORD(2, 1), &wsaData) != 0)
    		return kErrorWSAStartup;
    	char szHostName[MAX_PATH] = {0};
    	int nRetCode;
    	nRetCode = gethostname(szHostName, sizeof(szHostName));
    	PHOSTENT hostinfo;
    	if (nRetCode != 0)
    		return WSAGetLastError();        
    	hostinfo = gethostbyname(szHostName);
    	local_ip = inet_ntoa(*(struct in_addr*)*hostinfo->h_addr_list);
    	WSACleanup();
    	return 1;
    }
    
    int GetInternetIP( std::string &Inernet_ip )
    {
    	Inernet_ip.resize(32);
    	TCHAR szTempPath[_MAX_PATH] = {0}, szTempFile[MAX_PATH] = {0};
    	std::string buffer;
    	GetTempPath(MAX_PATH, szTempPath);
    	UINT nResult = GetTempFileName(szTempPath, _T("~ex"), 0, szTempFile);
    	int ret=URLDownloadToFile(NULL,_T("http://www.ip138.com/ip2city.asp"),szTempFile,BINDF_GETNEWESTVERSION,NULL);
    	if (ret == S_FALSE)
    		return 0;
    	FILE *fp;
    	if (_wfopen_s(&fp,szTempFile,_T("rb"))!=0){
    		return 0;
    	}
    	fseek(fp,0,SEEK_END);//得到文件大小
    	int ilength=ftell(fp);
    	fseek(fp,0,SEEK_SET);
    	if(ilength>0)
    	{ 
    		buffer.resize(ilength);
    		fread(&buffer[0],sizeof(TCHAR),ilength,fp);
    		fclose(fp);
    		DeleteFile(_T("ip.ini"));
    		
    		 char* str_ip = strstr(&buffer[0], "[");
    		 if (str_ip !=NULL)
    		 {
    			 sscanf_s(str_ip+1, "%[^]]", &Inernet_ip[0], 32);
    		 }	 
    		return 1;
    	}
    	else
    	{
    		fclose(fp);
    		return 0;
    	}
    }

    解释
获取内网ip:
1、通过系统函数gethostname得到主机名。
2、通过主机名得到内网ip。
获取外网ip:
1、通过ip源,下载其脚本文件并保存。
2、保存文件中有外网ip,解析出来。

获取外网ip所需要的IP源:(几乎所有可以显示IP地址的网页都可以成为IP源

http://www.ipchicken.com/ 

http://whatismyipaddress.com/ 

http://www.ip138.com/ip2city.asp


  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
以下是使用 C++ Builder 11 连接外网 IP SQL Server 数据库的步骤: 1. 首先,你需要确保你的 SQL Server 数据库可以通过外网 IP 地址访问。你可以通过配置 SQL Server 的网络配置和防火墙规则来实现。 2. 打开 C++ Builder 11,创建一个新项目。 3. 在项目中添加一个 TADOConnection 组件。TADOConnection 是用于连接到数据库的组件。 4. 在 TADOConnection 的属性中,设置 ConnectionString 属性以指定连接字符串。连接字符串应该包括 SQL Server 实例名称、数据库名称、用户名和密码等信息。而外网 IP 地址应该在 Data Source 属性中指定。例如: ``` Provider=SQLOLEDB.1;Persist Security Info=False;User ID=your_username;Password=your_password;Initial Catalog=your_database;Data Source=xxx.xxx.xxx.xxx; ``` 其中,xxx.xxx.xxx.xxx 是你的 SQL Server 数据库所在的外网 IP 地址。 5. 在 TADOConnection 的属性中,设置 LoginPrompt 属性为 false,以禁用登录提示框。 6. 在代码中,使用 TADOConnection 的 Connected 属性连接到数据库: ``` ADOConnection1->Connected = true; ``` 7. 连接成功后,你可以使用 TADOQuery 或 TADOTable 组件来查询和操作数据库中的数据。 以上就是使用 C++ Builder 11 连接外网 IP SQL Server 数据库的基本步骤。注意,实际连接字符串的具体格式可能会因为 SQL Server 版本和配置而异。另外,使用外网 IP 连接数据库需要注意安全性问题,建议加强数据库用户权限和密码保护等措施。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值