Advertisement
MdSadmanSiraj

get_webpage.c

Jul 25th, 2022 (edited)
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.31 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h> /* close() */
  5. #include <sys/socket.h>
  6. #include <netdb.h>
  7.  
  8. int main(void)
  9. {
  10.     int sock;
  11.     char host[] = "http://52.9.99.142/set_points.php";
  12.     char port[] = "80";
  13.     struct addrinfo hints, *res;
  14.     char message[] = "GET / HTTP/1.1\nHost: http://52.9.99.142/set_points.php\n\n";
  15.     unsigned int i;
  16.     char buf[1024];
  17.     int bytes_read;
  18.     int status;
  19.  
  20.     memset(&hints, 0, sizeof hints);
  21.     hints.ai_family = AF_INET;
  22.     hints.ai_socktype = SOCK_STREAM;
  23.     status = getaddrinfo(host, port, &hints, &res);
  24.     if (status != 0) {
  25.         perror("getaddrinfo");
  26.         return 1;
  27.     }
  28.     sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  29.     if (sock == -1) {
  30.         perror("socket");
  31.         return 1;
  32.     }
  33.     status = connect(sock, res->ai_addr, res->ai_addrlen);
  34.     if (status == -1) {
  35.         perror("connect");
  36.         return 1;
  37.     }
  38.     freeaddrinfo(res);
  39.     send(sock, message, strlen(message), 0);
  40.  
  41.     do {
  42.         bytes_read = recv(sock, buf, 1024, 0);
  43.         if (bytes_read == -1) {
  44.             perror("recv");
  45.         }
  46.         else {
  47.             printf("%.*s", bytes_read, buf);
  48.         }
  49.     } while (bytes_read > 0);
  50.  
  51.     close(sock);
  52.  
  53.     return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement