Guest User

Untitled

a guest
Jul 15th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.32 KB | None | 0 0
  1. #include <errno.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <err.h>
  7. #include <sys/types.h>
  8. #include <sys/socket.h>
  9. #include <netdb.h>
  10. #include <arpa/inet.h>
  11.  
  12. /* "BSIZE" adalah ukuran buffer kita menggunakan untuk membaca dari soket. */
  13.  
  14. #define BSIZE 0x1000
  15.  
  16. /* Dapatkan halaman web dan mencetaknya ke output standar. */
  17.  
  18. static void get_page (int s, const char * host, const char * page)
  19. {
  20.     char * msg;
  21.  
  22. /* "Format" adalah format permintaan HTTP kita mengirim ke server web. */
  23.  
  24.     const char * format =
  25.         "GET /%s HTTP/1.0\r\nHost: %s\r\nUser-Agent: fetch.c\r\n\r\n";
  26.     asprintf (& msg, format, page, host);
  27.     send (s, msg, strlen (msg), 0);
  28.     while (1) {
  29.         int bytes;
  30.         char buf[BSIZE+10];
  31.         bytes = recvfrom (s, buf, BSIZE, 0, 0, 0);
  32.         if (bytes == -1) {
  33.             fprintf (stderr, "%s\n", strerror(errno));
  34.             exit (1);
  35.         }
  36.         buf[bytes] = '\0';
  37.         printf ("%s", buf);
  38.         if (bytes == 0) {
  39.             break;
  40.         }
  41.     }
  42.     free (msg);
  43. }
  44.  
  45. int main ()
  46. {
  47.     struct addrinfo hints, *res, *res0;
  48.     int error;
  49.  
  50.     /* "S" adalah file descriptor dari soket. */
  51.  
  52.     int s;
  53.  //   char npm[10];
  54.  //   printf ("Masukkan Npm Anda :");
  55.  //   scanf ("%s", npm);
  56.     /* Dapatkan salah satu halaman web di sini. */
  57.     const char * npm  = ("13110921");
  58.     const char * host = ("student.gunadarma.ac.id");
  59.  
  60.     memset (&hints, 0, sizeof(hints));
  61.  
  62.     /* Jangan menentukan jenis koneksi internet. */
  63.  
  64.     hints.ai_family = PF_UNSPEC;
  65.     hints.ai_socktype = SOCK_STREAM;
  66.     error = getaddrinfo (host, "http", & hints, & res0);
  67.     if (error) {
  68.         fprintf (stderr, "%s\n", gai_strerror(error));
  69.         exit (1);
  70.     }
  71.     s = -1;
  72.     for (res = res0; res; res = res->ai_next) {
  73.         s = socket (res->ai_family, res->ai_socktype, res->ai_protocol);
  74.         if (s < 0) {
  75.             fprintf (stderr, "socket: %s\n", strerror (errno));
  76.             exit (1);
  77.         }
  78.         if (connect(s, res->ai_addr, res->ai_addrlen) < 0) {
  79.             fprintf (stderr, "connect: %s\n", strerror (errno));
  80.             close(s);
  81.             exit (1);
  82.         }
  83.         break;
  84.  
  85.  }
  86.     if (s != -1) {
  87.         get_page (s, host, "");
  88.     }
  89.     freeaddrinfo (res0);
  90.     return 0;
  91. }
Add Comment
Please, Sign In to add comment