Advertisement
Guest User

Untitled

a guest
Dec 12th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.08 KB | None | 0 0
  1. #include <sys/socket.h>
  2. #include <stdio.h>
  3. #include <netinet/in.h>
  4. #include <string.h>
  5. #include <arpa/inet.h>
  6. #include <unistd.h>
  7. #include <stdlib.h>
  8.  
  9. #define LENGTH 8092
  10.  
  11. int main(void) {
  12. char message[1024];
  13. struct sockaddr_in adr;
  14. int gniazdo;
  15. char separator[] = " ";
  16. char * token;
  17. char abcd[512];
  18. int port;
  19.  
  20. printf("Podaj adres IP odbiorcy: ");
  21. scanf("%s", abcd);
  22. printf("Podaj numer portu odbiorcy: ");
  23. scanf("%u", & port);
  24.  
  25. gniazdo = socket(PF_INET, SOCK_STREAM, 0);
  26.  
  27. adr.sin_family = AF_INET;
  28. adr.sin_port = htons(port);
  29. adr.sin_addr.s_addr = inet_addr(abcd);
  30.  
  31. if (connect(gniazdo, (struct sockaddr * ) & adr,
  32. sizeof(adr)) < 0) {
  33. printf("Nawiazanie polaczenia nie powiodlo sie.\n");
  34. return 1;
  35. }
  36.  
  37. printf("Polaczenie nawiazane.\n");
  38. while (1) {
  39. memset(message, 0, 1024);
  40. printf("Podaj message: \n");
  41. fflush(stdout);
  42. if (fgets(message, 1024, stdin) != NULL) {
  43. printf("-------------------- ");
  44. }
  45.  
  46. token = strtok(message, separator);
  47. printf("token: %s\n", token);
  48.  
  49. if (strncmp(token, "send", 4) == 0) {
  50. send(gniazdo, "send", 4, 0);
  51. //memset(token, '\0', sizeof(token));
  52. token = strtok(NULL, separator);
  53. printf("TOken22 %s\n", token);
  54. send(gniazdo, token, strlen(token), 0);
  55. char * fs_name;
  56. strcpy(fs_name, token);
  57. printf("fsname %s\n", fs_name);
  58. char sdbuf[LENGTH];
  59. printf("wywalka");
  60. printf("[Client] Sending %s to the Server...", fs_name);
  61. //get file length
  62. printf("wywalka");
  63. FILE * fileptr;
  64. char * buffer;
  65. long filelen;
  66. printf("wywalka");
  67. fileptr = fopen(fs_name, "rb"); // Open the file in binary mode
  68. printf("wywalka");
  69. fseek(fileptr, 0, SEEK_END); // Jump to the end of the file
  70. filelen = ftell(fileptr); // Get the current byte offset in the file
  71. printf("file length: %ld", filelen);
  72. char str[12];
  73. memset(str, '\0', 12);
  74. sprintf(str, "%d\n", filelen);
  75. buffer = (char * ) malloc((filelen + 1) * sizeof(char));
  76. send(gniazdo, str, strlen(str), 0);
  77. fclose(fileptr); // Close the file
  78.  
  79. FILE * fs = fopen(fs_name, "r");
  80. if (fs == NULL) {
  81. printf("ERROR: File %s not found.\n", token);
  82. exit(1);
  83. }
  84.  
  85. bzero(sdbuf, LENGTH);
  86. int fs_block_sz;
  87.  
  88. while ((fs_block_sz = fread(sdbuf, sizeof(char), LENGTH, fs)) > 0) {
  89. if (send(gniazdo, sdbuf, fs_block_sz, 0) < 0) {
  90. printf("ERROR: Failed to send file %s.\n", token);
  91. break;
  92. }
  93. bzero(sdbuf, LENGTH);
  94. }
  95. printf("Ok File %s from Client was Sent!\n", token);
  96. //send(gniazdo, token, strlen(token), 0);
  97. printf("message wyslana (message sent).\n");
  98. } else if (strncmp(token, "list", 4) == 0) {
  99. int numOfPaths;
  100. printf("wywolaj funkcje: list\n");
  101.  
  102. send(gniazdo, token, strlen(token), 0);
  103.  
  104. memset(message, '\0', 1024);
  105. recv(gniazdo, message, 1024, 0);
  106. sscanf(message, "%d", & numOfPaths);
  107. printf("%d paths(s): \n", numOfPaths);
  108.  
  109. for (int i = 0; i < numOfPaths; i++) {
  110. memset(message, '\0', 1024);
  111. recv(gniazdo, message, 1024, 0);
  112. printf("%s\n", message);
  113. }
  114. } else if (strncmp(token, "quit", 4) == 0) {
  115. send(gniazdo, token, strlen(token), 0);
  116. close(gniazdo);
  117. exit(0);
  118. } else {
  119. printf("Nieznana komenda!\n");
  120. }
  121. }
  122. close(gniazdo);
  123.  
  124.  
  125. /* Receive File from Server */
  126. /*printf("[Client] Receiveing file from Server and saving it as final.txt...");
  127. char* fr_name = "/home/aryan/Desktop/progetto/final.txt"; //to variable
  128. FILE *fr = fopen(fr_name, "a");
  129. if(fr == NULL)
  130. printf("File %s Cannot be opened.\n", fr_name);
  131. else
  132. {
  133. bzero(revbuf, LENGTH);
  134. int fr_block_sz = 0;
  135. int success = 0;
  136. //while(success == 0)
  137. //{
  138. while(fr_block_sz = recv(sockfd, revbuf, LENGTH, 0))
  139. {
  140. if(fr_block_sz < 0)
  141. {
  142. error("Receive file error.\n");
  143. }
  144. int write_sz = fwrite(revbuf, sizeof(char), fr_block_sz, fr);
  145. if(write_sz < fr_block_sz)
  146. {
  147. error("File write failed.\n");
  148. }
  149. else if(fr_block_sz)
  150. {
  151. break;
  152. }
  153. bzero(revbuf, LENGTH);
  154. }
  155. printf("Ok received from server!\n");
  156. success = 1;
  157. fclose(fr);
  158. //}
  159. }*/
  160. return 0;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement