当前位置:网站首页>通过ip获取归属地
通过ip获取归属地
2022-07-17 06:45:00 【qq_857305819】
ip.plyz.net 提供了ip查询功能,只要发送一个get请求就行了
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <WinSock2.h>
#include <iostream>
#include <stdio.h>
#include <string>
#pragma comment(lib,"Ws2_32.lib")
using namespace std;
int UTF82GBK(char* szUtf8, char* szGbk, int Len)
{
int n = MultiByteToWideChar(CP_UTF8, 0, szUtf8, -1, NULL, 0);
WCHAR* wszGBK = new WCHAR[sizeof(WCHAR) * n];
memset(wszGBK, 0, sizeof(WCHAR) * n);
MultiByteToWideChar(CP_UTF8, 0, szUtf8, -1, wszGBK, n);
n = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL);
if (n > Len)
{
delete[]wszGBK;
return -1;
}
WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, szGbk, n, NULL, NULL);
delete[]wszGBK;
wszGBK = NULL;
return 0;
}
bool getinfo()
{
bool Ret = false;
const char* hostname = "ip.plyz.net";
struct hostent* host = gethostbyname(hostname);
if (host==nullptr)
{
return false;
}
/* 初始化一个连接服务器的结构体 */
sockaddr_in serveraddr;
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(80);
/* 此处也可以不用这么做,不需要用gethostbyname,把网址ping一下,得出IP也是可以的 */
serveraddr.sin_addr.S_un.S_addr = *((int*)*host->h_addr_list);
int sock = socket(AF_INET, SOCK_STREAM, 0);
do
{
if (sock == -1)
{
break;
}
if (::connect(sock, (struct sockaddr*)&serveraddr, sizeof(sockaddr_in)) == -1)
{
break;
}
/* GET请求 */
const char* bufSned = "GET http://ip.plyz.net/ip.ashx?ip=myip\r\n";
/* 发送GET请求 */
if (send(sock, bufSned, strlen(bufSned), 0) == 0)
{
break;
}
char BufRecv[100] = {
};
int nLen = 0;
string HtmlData;
/* 开始接收数据 */
while ((nLen = recv(sock, BufRecv, 100, 0)) > 0)
{
/* 把数组拼接成string类型,方便下面的处理 */
HtmlData += BufRecv;
}
char data[100] = {
0 };
UTF82GBK(BufRecv, data, 100);
Ret = true;
printf("BufRecv=%s\n", data);
} while (false);
if (sock!=INVALID_SOCKET)
{
closesocket(sock);
}
return Ret;
}
int main()
{
/* 初始化 */
WSADATA wsdata;
WSAStartup(MAKEWORD(1, 1), &wsdata);
getinfo();
return 0;
}
边栏推荐
- Flowable query, complete, void, delete tasks
- 1669. Merge two linked lists (merge of two linked lists)
- Shenzhen Prudential written examination record
- 【flask入门系列】异常处理
- Visit Beijing Zoo in dog days
- CAD fill to polyline script
- Wechat oauth2.0 login process and security analysis
- Xinlinx zynq7010 domestic replacement fmql10s400 national production arm core board + expansion board
- @How can conditionalonmissingbean cover beans in third-party components
- Redis 跳跃表实现原理 & 时间复杂度分析
猜你喜欢

Modify the select style

分叉币的发展史及价值|ETH、BCH、BSV 2020-03-08
![[C# 变量常量关键字]- C# 中的变量常量以及关键字](/img/9b/433f9110d9c7599d8beac8288ea409.png)
[C# 变量常量关键字]- C# 中的变量常量以及关键字

Titanic passenger rescue prediction (Advanced)

Question 114 of Li Kou: binary tree expansion linked list

Ku115 FPGA high performance 10G Optical fiber network hardware accelerator card / 2-way 10G Optical fiber data accelerator card

4-channel FMC interface baseband signal processing board (2 FMC interfaces, 2 fmc+ interfaces)

How to check whether the app has user information and data leakage vulnerabilities

STM32F103C8T6硬件IIC控制4针0.96寸OLED显示屏

xgboos-hperopt
随机推荐
《牛客刷题》sql错题集
Go语言圣经
Download, installation and use of mongodb
4-channel FMC interface baseband signal processing board (2 FMC interfaces, 2 fmc+ interfaces)
Xilinx ultrascale+ MPSoC (zu9eg/zu15eg) high performance PCIe data preprocessing board
VMware cloud director 10.4 release (including download) - cloud computing provisioning and management platform
1669. Merge two linked lists (merge of two linked lists)
Is it necessary to buy pension insurance? What are the pension products suitable for the elderly?
深圳保诚笔试记录
牛客题目——打家劫舍一、打家劫舍二
Database review -- database recovery technology
redis数据持久化
[C language] user defined type details: structure, enumeration, union
半导体材料技术
[C# Console]-C# 控制台类
[C # console] - C # console class
the max_ iter was reached which means the coef_ did not converge “the coef_ did not converge“
通过Dao投票STI的销毁,SeekTiger真正做到由社区驱动
【C语言】自定义类型详解:结构体、枚举、联合
A set of Jenkins style for personal use