Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <dirent.h>
  5. #include <sys/stat.h>
  6. #include <sys/mman.h>
  7. #include <fcntl.h>
  8. #include <string.h>
  9. #include <errno.h>
  10. #include <ctype.h>
  11. #include <sys/types.h>
  12. #include <math.h>
  13. #include <sys/types.h>
  14. #include <sys/socket.h>
  15. #include <netinet/in.h>
  16. #include <netdb.h>
  17. #include <endian.h>
  18.  
  19.  
  20. // use ./client paris.kocachic.com 2280 23 43 43
  21.  
  22. //Global Variables
  23.  
  24. //Function prototypes
  25.  
  26. //Functions
  27.  
  28. //This function prints all the arguments in the command line
  29. void printArguments(int argc, char **argv){
  30. printf("Number of arguments including the filename %d\n", argc);
  31. // Print the elements of the array argv
  32. printf("The elements of the array are:\n");
  33. for (int i = 0; i < (int)(argc - 1) ; ++i) {
  34. printf("%s\n", argv[i + 1]); //Plus one so as not to print './'
  35. }
  36. }
  37.  
  38. int main(int argc, char **argv){
  39. printArguments(argc, argv);
  40. printf("%s\n", argv[0]);
  41.  
  42. //printf("The output of argv[argc-3]: %d", (int)*argv[argc -3] ); //This print system call returns 50 which the ascci code of 2
  43.  
  44. long x1Long = strtol(argv[argc -3], NULL, 10);
  45. long x2Long = strtol(argv[argc -2], NULL, 10);
  46. long x3Long = strtol(argv[argc -1], NULL, 10);
  47.  
  48.  
  49. long thePortLong = strtol(argv[2], NULL, 10);
  50.  
  51.  
  52. uint16_t thePort = (uint16_t)thePortLong;
  53.  
  54.  
  55. int sock;
  56. if ( ( sock = socket(PF_INET, SOCK_STREAM, 0) ) < 0 ) {
  57. printf("\n Socket creation error \n");
  58. return -1;
  59. }
  60.  
  61. struct sockaddr_in addr = {
  62.  
  63. .sin_family = AF_INET,
  64.  
  65. .sin_port = htobe16(thePort),
  66.  
  67. };
  68.  
  69. struct hostent *host = gethostbyname(argv[1]);
  70.  
  71. // copy the address into the addr struct
  72.  
  73. memcpy(&addr.sin_addr.s_addr, host->h_addr, host->h_length);
  74.  
  75. if ( ( connect( sock, (struct sockaddr *)&addr, sizeof addr ) ) < 0)
  76. {
  77. printf("\nConnection Failed \n");
  78. return -1;
  79. }
  80.  
  81.  
  82. int amountOfNumbersToSend = 3;
  83. // number we want to send
  84. uint64_t xsend0 = htobe64(amountOfNumbersToSend);
  85. write(sock, &xsend0, sizeof xsend0);
  86.  
  87. int x1 = (int)x1Long ;
  88. // number we want to send
  89. uint64_t xsend1 = htobe64(x1);
  90. write(sock, &xsend1, sizeof xsend1);
  91.  
  92. int x2 = (int)x2Long;
  93. // number we want to send
  94. uint64_t xsend2 = htobe64(x2);
  95. write(sock, &xsend2, sizeof xsend2);
  96.  
  97. int x3 = (int)x3Long ;
  98. // number we want to send
  99. uint64_t xsend3 = htobe64(x3);
  100. write(sock, &xsend3, sizeof xsend3);
  101.  
  102. uint64_t x_recv1;
  103. read(sock, &x_recv1, sizeof x_recv1);
  104. int r1 = be64toh(x_recv1);
  105.  
  106. uint64_t x_recv2;
  107. read(sock, &x_recv2, sizeof x_recv2);
  108. int r2 = be64toh(x_recv2);
  109.  
  110. uint64_t x_recv3;
  111. read(sock, &x_recv3, sizeof x_recv3);
  112. int r3 = be64toh(x_recv3);
  113.  
  114. printf("The output %d\n", r1);
  115. printf("The output %d\n", r2);
  116. printf("The output %d\n", r3);
  117.  
  118. return 0;
  119. }
  120.  
  121. /*
  122. Notes
  123.  
  124.  
  125. //These lines of codes are for the server
  126.  
  127. struct sockaddr_in listening_addr = {
  128.  
  129. .sin_family = AF_INET, // IPv4
  130.  
  131. .sin_port = htobe16(argv[2]), // listen on port 3000
  132.  
  133. .sin_addr = { // listen on any/every interface
  134.  
  135. .s_addr = htobe32(INADDR_ANY),
  136.  
  137. },
  138.  
  139. };
  140.  
  141. //bind(sock, (struct sockaddr *)&listening_addr, sizeof listening_addr);
  142.  
  143.  
  144.  
  145. listen(sock, 2);
  146.  
  147. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement