小弟我的openwrt学习笔记(二十一):广域网IP地址获取_socket http

我的openwrt学习笔记(二十一):广域网IP地址获取_socket http

我的openwrt学习笔记(二十一):广域网IP地址获取_socket http


在实际的LINUX 开发中,有时候需要获取当前设备所在网络的IP地址信息,上传到服务器等,获取网络的IP地址有很多种方式。

     包含Json格式的IP地址等信息,是常用的一种方式,一般需要使用包含http 协议的 curl 工具最为方便了。

     也可以使用sockethttp格式的通信,进行获取

linux@ubuntu:~/ubuntu1/socket_http$ ls -l

total 28

-rwxrwxr-x 1 linux linux 10421  8月 26 13:25 a.out

-rwxrwxr-x 1 linux linux     0  8月 26 13:27 getip

-rw-rw-r-- 1 linux linux  1435  8月 26 13:27 getip.c

-rw-rw-r-- 1 linux linux    61  6月 29 09:45 ip.txt

-rwxr-xr-x 1 linux linux   460  6月 24 15:37 Lindent

-rw-rw-r-- 1 linux linux  1817  8月 26 13:27 readipfile.c

linux@ubuntu:~/ubuntu1/socket_http$ ./getip

linux@ubuntu:~/ubuntu1/socket_http$ cat ip.txt

当前 IP:115.195.46.57 来自:浙江省杭州市 电信

linux@ubuntu:~/ubuntu1/socket_http$ ./getip

linux@ubuntu:~/ubuntu1/socket_http$ ./a.out

当前 IP:115.195.46.57 来自:浙江省杭州市 电信

substr=IP:115.195.46.57 来自:浙江省杭州市 电信

current wlan ip:115.195.46.57

 

ipstrbuf=115.195.46.57

 Getip.c源代码

linux@ubuntu:~/ubuntu1/socket_http$ cat getip.c

#include <netdb.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#include <arpa/inet.h>

#include <netinet/in.h>

#include <sys/socket.h>

typedef enum {

         false,

         true

} bool;

int main()

{

         bool flag;

         int sock;

         char **pptr = NULL;

         struct sockaddr_in destAddr;

         struct hostent *ptr = NULL;

         char destIP[128];

         char szBuffer[] =

             {

      "GET /ip2city.asp HTTP/1.1\r\nHost:www.ip138.com\r\nConnection:Close\r\n\r\n"

  };

         char res[1024];

 

         sock = socket(AF_INET, SOCK_STREAM, 0);

         if (-1 == sock) {

                   perror("creat socket failed");

                   exit(0);

         }

 

         bzero((void *)&destAddr, sizeof(destAddr));

         destAddr.sin_family = AF_INET;

         destAddr.sin_port = htons(80);

 

         ptr = gethostbyname("www.ip138.com");

         if (NULL == ptr) {

                   perror("gethostbyname error");

                   exit(0);

         }

         for (flag = false, pptr = ptr->h_addr_list; NULL != *pptr; ++pptr) {

                   inet_ntop(ptr->h_addrtype, *pptr, destIP, sizeof(destIP));

                   printf("ip138 addr:%s\n", destIP);

                   destAddr.sin_addr.s_addr = inet_addr(destIP);

                   if (-1 !=

                       connect(sock, (struct sockaddr *)&destAddr,

                                sizeof(struct sockaddr))) {

                            flag = true;

                            break;

                   }

         }

 

         if (false == flag) {

                   perror("connect failed");

         }

 

         if (strlen(szBuffer) != send(sock, szBuffer, strlen(szBuffer), 0)) {

                   perror("send error");

                   exit(0);

         }

 

         if (-1 == recv(sock, res, 1024, 0)) {

                   perror("recv error");

                   exit(0);

         }

 

         printf("wlan ip res:\n%s\n", res);

         return 0;

}

 

版权声明:本文为博主原创文章,未经博主允许不得转载。