互亿无线 · 文档中心

IP地址查询 - 代码示例


C++对接IP地址查询接口DEMO示例
本文为您提供了C++语言版本的IP地址查询对接DEMO示例

IP地址查询接口文档 点击下载

IP地址查询接入指南 点击访问


// 接口类型:互亿无线IP地址查询接口
// 账户注册:https://user.ihuyi.com/register.html
// 注意事项:
// DEMO仅作参考
//linux下的编译:g++ ./test.cpp -o ./test
//linux下的执行:chmod +x test &&./test

#include <arpa/inet.h>
#include 
#include 
#include <netinet/in.h>
#include 
#include 
#include 
#include 
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include 
#include 

#define SA struct sockaddr
#define MAXLINE 4096
#define MAXSUB  2000
#define MAXPARAM 2048
#define LISTENQ 1024

extern int h_errno;

int basefd;
char *hostname = "api.ihuyi.com";
char *submit_uri = "/appdev/ip/Submit.json";

/**
* 发http post请求
*/
ssize_t http_post(char *page, char *poststr)
{
    char sendline[MAXLINE + 1], recvline[MAXLINE + 1];
    ssize_t n;
    snprintf(sendline, MAXSUB,
        "POST %s HTTP/1.0
"
        "Host: %s
"
        "Content-type: application/x-www-form-urlencoded
"
        "Content-length: %zu

"
        "%s", page, hostname, strlen(poststr), poststr);

    write(basefd, sendline, strlen(sendline));
    while ((n = read(basefd, recvline, MAXLINE)) > 0) {
        recvline[n] = "";
        printf("%s", recvline);
    }
    return n;
}

/**
* 提交认证
*/
ssize_t submit(char *account, char *password, char *ip)
{
    char params[MAXPARAM + 1];
    char *cp = params;
    sprintf(cp,"account=%s&password=%s&ip=%s", account, password, ip);
    return http_post(submit_uri, cp);
}

int  socked_connect(char *arg)
{
    struct sockaddr_in their_addr = {0};
    char buf[1024] = {0};
    char rbuf[1024] = {0};
    char pass[128] = {0};
    struct hostent *host = NULL;

    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if(sockfd<0)
    {
        printf ("create the sockfd is failed
");
        return -1;
    }

    if((host = gethostbyname(arg))==NULL)  
    {  
        printf("Gethostname error, %s
");  
        return -1; 
    }

    memset(&their_addr, 0, sizeof(their_addr));  
    their_addr.sin_family = AF_INET;  
    their_addr.sin_port = htons(80);  
    their_addr.sin_addr = *((struct in_addr *)host->h_addr);
    if(connect(sockfd,(struct sockaddr *)&their_addr, sizeof(struct sockaddr)) < 0)
    {
        close(sockfd);
        return  -1;
    }
    printf ("connect is success
");
    return sockfd;
    
}

int main(void)
{
    struct sockaddr_in servaddr;
    char str[50];

    #if 0
    //建立socket连接
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_addr =*(hostname);
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(80);
    inet_pton(AF_INET, str, &servaddr.sin_addr);
    connect(sockfd, (SA *) & servaddr, sizeof(servaddr));
    #endif
    
    if((basefd= socked_connect(hostname))==-1)
    {
        printf("connect is failed
");
        return -1;
    }
    printf("basefd is =%d
",basefd);

    char *account = "APIID";
    char *password = "APIKEY";
    char *ip = "192.168.1.1";

    /**************** 提交认证 *****************/
    submit(account, password, ip);
    printf("submit is success
");
    close(basefd);
    exit(0);
}

现在注册,即享新用户礼包!

在线咨询
电话咨询

服务热线:

4008 808 898

服务热线(工作时间):

4008 808 898

业务咨询(非工作时间):

售后咨询(非工作时间):

验证码已发送到您的手机,请查收!

输入验证码后,点击“开通体验账户”按钮可立即开通体验账户。

收不到短信验证码?
互亿无线不提供接码服务,接码用户请勿点击开通按钮。
×