Linux网络编程:获取本机的公网IP


Linux网络编程:获取本机的公网IP

[cpp]   
  1.  #include <stdio.h>
  2. #include <string.h>   
  3. #include <sys/types.h>   
  4. #include <errno.h>   
  5. #include <sys/stat.h>   
  6. #include <fcntl.h>   
  7. #include <unistd.h>   
  8. #include <stdlib.h>   
  9. #include <netdb.h>   
  10. #include <stddef.h>   
  11. #include <netinet/in.h>   
  12. #include <arpa/inet.h>   
  13. #include <sys/socket.h>   
  14.   
  15. #define BUF_SIZE   512   
  16.   
  17. int port = 80;  
  18.   
  19. void getip(char *url)  
  20. {  
  21.     struct sockaddr_in pin;  
  22.     struct hostent *nlp_host;  
  23.     int sd = 0;  
  24.     int len = 0;  
  25.     int i, count = 0;  
  26.     int recv_start = 0, recv_end = 0;  
  27.     char buf[BUF_SIZE] = { 0 }, myurl[100] =  
  28.     {  
  29.     0};  
  30.     char host[100] = { 0 }, GET[100] =  
  31.     {  
  32.     0}, header[240] =  
  33.     {  
  34.     0};  
  35.     char *pHost = 0;  
  36.   
  37.     ///get the host name and the relative address from url name!!!   
  38.     strcpy(myurl, url);  
  39.     for (pHost = myurl; *pHost != '/' && *pHost != '\0'; ++pHost) ;  
  40.     if ((int)(pHost - myurl) == strlen(myurl))  
  41.         strcpy(GET, "/");  
  42.     else  
  43.         strcpy(GET, pHost);  
  44.     *pHost = '\0';  
  45.     strcpy(host, myurl);  
  46.   
  47.     ///setting socket param   
  48.     if ((nlp_host = gethostbyname(host)) == 0)  
  49.     {  
  50.         perror("error get host\n");  
  51.         exit(1);  
  52.     }  
  53.   
  54.     bzero(&pin, sizeof(pin));  
  55.     pin.sin_family = AF_INET;  
  56.     pin.sin_addr.s_addr = htonl(INADDR_ANY);  
  57.     pin.sin_addr.s_addr = ((struct in_addr *)(nlp_host->h_addr))->s_addr;  
  58.     pin.sin_port = htons(port);  
  59.   
  60.     if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1)  
  61.     {  
  62.         perror("Error opening socket!!!\n");  
  63.         exit(1);  
  64.     }  
  65.   
  66.     ///together the request info that will be sent to web server   
  67.     ///Note: the blank and enter key byte is necessary,please remember!!!   
  68.     strcat(header, "GET");  
  69.     strcat(header, " ");  
  70.     strcat(header, GET);  
  71.     strcat(header, " ");  
  72.     strcat(header, "HTTP/1.1\r\n");  
  73.     strcat(header, "HOST:");  
  74.     strcat(header, host);  
  75.     strcat(header, "\r\n");  
  76.     strcat(header, "ACCEPT:*/*");  
  77.     strcat(header, "\r\nConnection: close\r\n\r\n\r\n");  
  78.   
  79.     ///connect to the webserver,send the header,and receive the web sourcecode     
  80.     if (connect(sd, (void *)&pin, sizeof(pin)) == -1)  
  81.         printf("error connect to socket\n");  
  82.   
  83.     if (send(sd, header, strlen(header), 0) == -1)  
  84.     {  
  85.         perror("error in send \n");  
  86.         exit(1);  
  87.     }  
  88.   
  89.     ///send the message and wait the response!!!   
  90.     len = recv(sd, buf, BUF_SIZE, 0);  
  91.     if (len < 0)  
  92.         printf("receive data error!!!\n");  
  93.     else  
  94.     {  
  95.         printf("%s", &(buf[157]));  
  96.     }  
  97.     close(sd);  
  98.   
  99. }  
  100.   
  101. int main()  
  102. {  
  103.     char *url = "www.3322.org/dyndns/getip";  
  104.     getip(url);  
  105.     return 0;  
  106. }  

运行结果:

  1. [root@www.bkjia.com test]#  
  2. [root@www.bkjia.com test]# gcc getIP.c -o getIP  
  3. [root@www.bkjia.com test]# ./getIP   
  4. 218.13.34.138  
  5. [root@www.bkjia.com test]#   

相关内容