c++ 获取外网ip地址
// 外部调用 ==============================================
networkip.GetLocalIP();
string retip = networkip.GetInternetIP();// 封装实现 ==============================================
class getnetworkip
{
public:
getnetworkip();
~getnetworkip();std::string GetLocalIP();
std::string GetInternetIP();
private:
};//
#include <WINSOCK2.H>
#include <urlmon.h>
#include <string>
using namespace std;#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "urlmon.lib")#define MAX_SIZE 1024getnetworkip::getnetworkip()
{
}getnetworkip::~getnetworkip()
{
}std::string getnetworkip::GetLocalIP()
{
WSADATA wsaData;
int err = WSAStartup(MAKEWORD(2, 0), &wsaData);
if (err != 0)
{
return "err";
}char szHostName[MAX_PATH] = { 0 };
int nRetCode;
nRetCode = gethostname(szHostName, sizeof(szHostName));char* lpLocalIP;
PHOSTENT hostinfo;if (nRetCode != 0)
{
return "errcode";
}hostinfo = gethostbyname(szHostName);
lpLocalIP = inet_ntoa(*(struct in_addr*)*hostinfo->h_addr_list);if (szHostName != NULL)
{
printf("主机名: %s\n", szHostName);
printf("本地IP: %s\n", lpLocalIP);
}WSACleanup();return lpLocalIP;
}std::string getnetworkip::GetInternetIP()
{
char buf[2048] = { 0 }; //把网页中读出的数据放在此处
char chURL[128] = { "http://www.whatismyip.com.tw/" };//将网页数据写入c:\i.ini文件中
URLDownloadToFileA(0, chURL, "c:\\i.ini", 0, NULL);string str_ip("");
FILE *fp = fopen("c:\\i.ini", "rb+");
if (fp != NULL)
{
//
fseek(fp, 0, SEEK_SET);
fread(buf, 2048, 1, fp);
fclose(fp);//在buf中查找 [ 的位置, iIndex是buf中从[开始剩下的字符串,包括[这个字符串 ==
string str = buf;
int nstart = str.find("<h2>");
int nend = str.find("</h2>");
int lenth = nend - nstart - 4;str_ip = str.substr(nstart + 4, lenth);
}remove("c:\\i.ini");return str_ip;
}
c++ 获取外网ip地址相关推荐
- python获取公网ip的几种方式_python获取外网ip地址的方法总结
本文实例总结了python获取外网ip地址的方法.分享给大家供大家参考.具体如下: 一.利用脚本引擎库直接获取 import console; import web.script import ine ...
- 如何从HttpServletRequest中获取外网IP地址
如何从HttpServletRequest中获取外网IP地址 Java 中可以通过HttpServletRequest类的getRemoteAddr(); //可能是用户真是ip也可能是代理服务器的i ...
- 如何获取外网IP地址
1. 获取外网IP的方法: 获取外网IP地址方法: 访问网站:http://www.3322.org/dyndns/getip 访问网站:http://city.ip138.com/ip2city. ...
- 获取外网IP地址API
1.获取外网IP地址 地址: http://pv.sohu.com/cityjson?ie=utf-8 返回结果: var returnCitySN = {"cip": " ...
- Python定时获取外网IP地址并发送邮件
去年入手了树莓派4B,放在家里当服务器用.由于电信公网IP经常更换,所以需要定期获取公网IP才能连上. import os import threading from smtplib import S ...
- MFC 获取外网IP地址和MAC地址
MAC地址获取: CString GetMacAddress(void) { //CString strIP,strGateWay,strSubnetMask; CString strMac; str ...
- MFC C++ 获取外网IP地址
#include <afxinet.h>//GB2312 转换成 Unicode wchar_t* GB2312ToUnicode(const char* szGBString) {UIN ...
- [网络]_获取内外网IP地址【Auto.js】
auto(); console.show();importClass('java.net.Inet4Address'); importClass('java.net.InetAddress'); im ...
- 怎样查询本机ip地址?如何利用花生壳获取外网IP教程
听到ip地址这个概念,想必大家都不会陌生,如果我们想要跟对方进行通信,通常我们需要知道对方的ip地址,另外在网络通信中,还有一个非常重要的地址,那就是mac地址.那么我们如何知道本机ip地址呢?我们如 ...
- Golang 获取当前外网IP/地址/运营商
Golang 获取当前外网IP/地址/运营商 - Go语言中文网 - Golang中文社区 package mainimport ("fmt""io/ioutil&quo ...
最新文章
- linux===Ubuntu 上安装 Node.js
- Python 学习笔记13 类 - 继承
- iOS中autolaylout和sizeclass的理解
- 开发相关手册、STM32各种库文件、相关软件、工具连接等(不断更新)
- 如何实现网络负荷分担
- 年入近1亿,曾被20家VC拒绝,如今电商是他大客户,数据处处有黄金
- android按钮响应事件吗,Android 按钮响应事件的几种方式
- 上位机通过串口获取单片机数据
- Unity2020.1新功能探路:编辑器相关更新
- 机器学习十大经典算法——线性回归
- Atitit 知识图谱解决方案:提供完整知识体系架构的搜索与知识结果overview
- CBoard框架使用总结五--扩展菜单配置功能
- 轴承过盈量程序matlab,轴承matlab处置程序.doc
- JAVA九宫格拼图游戏怎么计时_九宫格拼图怎么拼 如何玩转九宫格拼图游戏
- 如何在Mac电脑中获取最高权限删除顽固文件?
- 如何下载macOS Ventura、Monterey、Big Sur、Catalina Mojave完整安装程序
- 谈谈谈zookeeper
- WINVNC源码分析(四)
- 超高精度时间频率同步及其应用
- *基于RT-Thread的战舰开发板连接Onenent云平台(学习笔记)**