Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. #include <stdio.h> /* printf, sprintf */
  2. #include <stdlib.h> /* exit, atoi, malloc, free */
  3. #include <unistd.h> /* read, write, close */
  4. #include <string.h> /* memcpy, memset */
  5. #include <sys/socket.h> /* socket, connect */
  6. #include <netinet/in.h> /* struct sockaddr_in, struct sockaddr */
  7. #include <netdb.h> /* struct hostent, gethostbyname */
  8. #include <arpa/inet.h>
  9. #include "helpers.h"
  10. #include "requests.h"
  11.  
  12. int main(int argc, char *argv[])
  13. {
  14. int i;
  15. char *message;
  16. char *response;
  17. int sockfd;
  18.  
  19.  
  20. /*
  21. * Ex 0: Get cs.curs.pub.ro
  22. *
  23. * Pas 1: Se deschide conexiunea (open_connection)
  24. * Pas 2: Se creaza mesajul de request (compute_get_request)
  25. * Pas 3: Se trimite la server mesajul (send_to_server)
  26. * Pas 4: Se primeste raspuns de la server (receive_from_server)
  27. * Pas 5: Se inchide conexiunea cu serverul (close_connection)
  28. */
  29.  
  30. //TODO EX 0
  31. /*char ip[30];
  32. strcpy(ip, "141.85.241.51");
  33. sockfd = open_connection(ip, 80, AF_INET, SOCK_STREAM, 0);
  34.  
  35. message = compute_get_request(ip, "/", NULL);
  36.  
  37. send_to_server(sockfd, message);
  38.  
  39. response = receive_from_server(sockfd);
  40. printf("%s", response);
  41.  
  42. close_connection(sockfd);
  43. /*
  44. Ex 1: Get videos
  45. */
  46.  
  47. // TODO EX 1
  48. /*
  49. strcpy(ip, "185.118.200.37");
  50. sockfd = open_connection(ip, 8081, AF_INET, SOCK_STREAM, 0);
  51.  
  52. message = compute_get_request(ip, "/videos", NULL);
  53.  
  54. send_to_server(sockfd, message);
  55.  
  56. response = receive_from_server(sockfd);
  57. printf("%s", response);
  58. close_connection(sockfd);
  59. */
  60.  
  61. /*
  62. EX 2.1: Add video
  63. */
  64. char ip[30];
  65. /*strcpy(ip, "185.118.200.37");
  66. sockfd = open_connection(ip, 8081, AF_INET, SOCK_STREAM, 0);
  67. message = compute_post_request(ip, "/videos", "id=11&name=Videoclip");
  68. printf("%s", message);
  69.  
  70. send_to_server(sockfd, message);
  71.  
  72. response = receive_from_server(sockfd);
  73. printf("%s", response);
  74.  
  75. close_connection(sockfd);
  76. // TODO Ex 2.1
  77.  
  78.  
  79.  
  80. sockfd = open_connection(ip, 8081, AF_INET, SOCK_STREAM, 0);
  81. message = compute_get_request(ip, "/videos", NULL);
  82.  
  83. send_to_server(sockfd, message);
  84.  
  85. response = receive_from_server(sockfd);
  86. printf("%s", response);
  87.  
  88. close_connection(sockfd);
  89. /*
  90. Ex 2.2 Verificam noua colectie de videoclipuri
  91. */
  92.  
  93. // TODO Ex 2.2
  94. strcpy(ip, "185.118.200.37");
  95. sockfd = open_connection(ip, 8081, AF_INET, SOCK_STREAM, 0);
  96. message = compute_post_request(ip, "/weather/login", "username=admin&password=p@ss");
  97. //printf("%s", message);
  98.  
  99. send_to_server(sockfd, message);
  100.  
  101. response = receive_from_server(sockfd);
  102. printf("%s", response);
  103.  
  104. close_connection(sockfd);
  105.  
  106. /*
  107. Ex 3 Autentificare
  108. */
  109.  
  110. strcpy(ip, "185.118.200.37");
  111. sockfd = open_connection(ip, 8081, AF_INET, SOCK_STREAM, 0);
  112. message = compute_get_request(ip, "/weather/key", "isLogged=true");
  113. printf("%s", message);
  114.  
  115. send_to_server(sockfd, message);
  116.  
  117. response = receive_from_server(sockfd);
  118. printf("%s", response);
  119.  
  120. close_connection(sockfd);
  121.  
  122.  
  123.  
  124. strcpy(ip, "82.196.7.246");
  125. sockfd = open_connection(ip, 80, AF_INET, SOCK_STREAM, 0);
  126. message = compute_get_request(ip, "/data/2.5/weather", "q=bucharest&APPID=80f21fc11662726c0c3c8c5512d89b3a");
  127. printf("%s", message);
  128.  
  129. send_to_server(sockfd, message);
  130.  
  131. response = receive_from_server(sockfd);
  132. printf("%s", response);
  133.  
  134. close_connection(sockfd);
  135.  
  136.  
  137.  
  138.  
  139. /*strcpy(ip, "185.118.200.37");
  140. sockfd = open_connection(ip, 8081, AF_INET, SOCK_STREAM, 0);
  141. message = compute_get_request(ip, "/weather/key", "username=admin&password=p@ss");
  142. //printf("%s", message);
  143.  
  144. send_to_server(sockfd, message);
  145.  
  146. response = receive_from_server(sockfd);
  147. printf("%s", response);
  148.  
  149. close_connection(sockfd);
  150. // TODO Ex 3
  151. */
  152. free(message);
  153. return 0;
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement