Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <netdb.h>
  5. #include <netinet/in.h>
  6. #include <arpa/inet.h>
  7. #include <string.h>
  8. #include <limits.h>
  9. #include <unistd.h>
  10. #include <inttypes.h>
  11. #include <stdlib.h>
  12.  
  13. int main(int argc, char* argv[]) {
  14. char* host_name = argv[1];
  15. char* server_name = argv[2];
  16. char* key = argv[3];
  17.  
  18. struct addrinfo hints = {};
  19. hints.ai_family = AF_INET;
  20. hints.ai_socktype = SOCK_STREAM;
  21.  
  22. int error;
  23.  
  24. struct addrinfo* result;
  25.  
  26. error = getaddrinfo(host_name, server_name, &hints, &result);
  27. if (error) {
  28. printf("%s\n", gai_strerror(error));
  29. }
  30.  
  31. int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  32.  
  33. if (sock < 0) {
  34. return 0;
  35. }
  36.  
  37. if(connect(sock, result->ai_addr, result->ai_addrlen) != 0){
  38. return 0;
  39. }
  40.  
  41. if (send(sock, key, strlen(key), 0) <= 0) {
  42. return 0;
  43. }
  44.  
  45. //char num[128];
  46.  
  47. int k = 0;
  48.  
  49. if (recv(sock, &k, sizeof(int), 0) <= 0) {
  50. return 0;
  51. }
  52.  
  53. //int k = atoi(num);
  54.  
  55. for (int i = 0; i < k; i++) {
  56. /*char cur[128];
  57. sprintf(cur, "%d", i);*/
  58. if (send(sock, &i, sizeof(int), 0) <= 0) {
  59. return 0;
  60. }
  61. }
  62.  
  63. uint64_t res;
  64. //char s_res[128];
  65. if (recv(sock, &res, sizeof(uint64_t), 0) <= 0) {
  66. return 0;
  67. }
  68.  
  69. //res = strtoll(s_res, NULL, 10);
  70.  
  71. printf("%"PRIu64"\n", res);
  72.  
  73. freeaddrinfo(result);
  74. close(sock);
  75. return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement