歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux管理 >> Linux網絡 >> Linux網絡編程:獲取本機的公網IP

Linux網絡編程:獲取本機的公網IP

日期:2017/3/1 10:30:41   编辑:Linux網絡

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

運行結果:

  1. [[email protected] test]#
  2. [[email protected] test]# gcc getIP.c -o getIP
  3. [[email protected] test]# ./getIP
  4. 218.13.34.138
  5. [[email protected] test]#
Copyright © Linux教程網 All Rights Reserved