Advertisement
vegeta322

readwrite Server

Jan 21st, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h> //strlen
  3. #include <sys/socket.h>
  4. #include <arpa/inet.h> //inet_addr
  5. #include <unistd.h> //write
  6. #include <fcntl.h> //for open
  7. #include <unistd.h> //for close
  8. #include <stdbool.h>
  9. #include <stdlib.h>
  10.  
  11. #define DEFAULT_BUFLEN 512
  12. #define DEFAULT_PORT 27015
  13.  
  14. char* List(FILE *fp) {
  15. char data[DEFAULT_BUFLEN];
  16. char *lista = NULL;
  17. long filesize = ftell(fp);
  18. lista = malloc(filesize);
  19. while(fscanf(fp, "%s", data) == 1) {
  20. strcat(lista, data);
  21. strcat(lista, "\n");
  22. }
  23.  
  24. return lista;
  25. }
  26.  
  27. int WriteList(FILE *fp, char *data) {
  28. int flag = 0;
  29. char ime[DEFAULT_BUFLEN];
  30. while(fscanf(fp, "%s", ime) != EOF) {
  31. if(strcmp(ime, data) == 0) {
  32. flag = 1;
  33. printf("Datoteka sa istim imenom vec postoji\n");
  34. break;
  35. }
  36. }
  37. if(flag == 0) {
  38. fprintf(fp, "%s\n", data);
  39. printf("Uspesno upisana datoteka u listu\n");
  40. }
  41. return flag;
  42. }
  43.  
  44. int ReadList(FILE *fp, char *data) {
  45. int flag = 0;
  46. char ime[DEFAULT_BUFLEN];
  47. while(fscanf(fp, "%s", ime) != EOF) {
  48. if(strcmp(ime, data) == 0) {
  49. printf("Datoteka postoji, OK\n");
  50. break;
  51. }
  52. flag = 1;
  53. }
  54. return flag;
  55. }
  56.  
  57. //////////////////////////////////////// READ
  58. bool readdata(int sock, void *buf, int buflen)
  59. {
  60. unsigned char *pbuf = (unsigned char *) buf;
  61.  
  62. while (buflen > 0)
  63. {
  64. int num = recv(sock, pbuf, buflen, 0);
  65.  
  66. pbuf += num;
  67. buflen -= num;
  68. }
  69.  
  70. return true;
  71. }
  72.  
  73. bool readlong(int sock, long *value)
  74. {
  75. if (!readdata(sock, value, sizeof(value)))
  76. return false;
  77. *value = ntohl(*value);
  78. return true;
  79. }
  80.  
  81. bool readfile(int sock, FILE *f)
  82. {
  83. long filesize;
  84. if (!readlong(sock, &filesize))
  85. return false;
  86. if (filesize > 0)
  87. {
  88. char buffer[1024];
  89. do
  90. {
  91. int num;
  92. if(filesize > sizeof(buffer))
  93. num = sizeof(buffer);
  94. else
  95. num = filesize;
  96.  
  97. if (!readdata(sock, buffer, num))
  98. return false;
  99. int offset = 0;
  100. do
  101. {
  102. size_t written = fwrite(&buffer[offset], 1, num-offset, f);
  103. if (written < 1)
  104. return false;
  105. offset += written;
  106. }
  107. while (offset < num);
  108. filesize -= num;
  109. }
  110. while (filesize > 0);
  111. }
  112. return true;
  113. }
  114. //////////////////////////////////////// SEND
  115. bool senddata(int sock, void *buf, int buflen)
  116. {
  117. unsigned char *pbuf = (unsigned char *) buf;
  118.  
  119. while (buflen > 0)
  120. {
  121. int num = send(sock, pbuf, buflen, 0);
  122.  
  123. pbuf += num;
  124. buflen -= num;
  125. }
  126.  
  127. return true;
  128. }
  129.  
  130. bool sendlong(int sock, long value)
  131. {
  132. value = htonl(value);
  133. return senddata(sock, &value, sizeof(value));
  134. }
  135.  
  136. bool sendfile(int sock, FILE *f)
  137. {
  138. fseek(f, 0, SEEK_END);
  139. long filesize = ftell(f);
  140. rewind(f);
  141. if (filesize == EOF)
  142. return false;
  143. if(!sendlong(sock, filesize))
  144. return false;
  145. if (filesize > 0)
  146. {
  147. char buffer[1024];
  148. do
  149. {
  150. size_t num;
  151. if(filesize > sizeof(buffer))
  152. num = sizeof(buffer);
  153. else
  154. num = filesize;
  155. num = fread(buffer, 1, num, f);
  156. if (num < 1)
  157. return false;
  158. if (!senddata(sock, buffer, num))
  159. return false;
  160. filesize -= num;
  161. }
  162. while (filesize > 0);
  163. }
  164. return true;
  165. }
  166.  
  167.  
  168. int main(int argc , char *argv[])
  169. {
  170. int socket_desc , client_sock, c , read_size;
  171. struct sockaddr_in server , client;
  172. char client_message[DEFAULT_BUFLEN];
  173.  
  174. //Create socket
  175. socket_desc = socket(AF_INET , SOCK_STREAM , 0);
  176. if (socket_desc == -1)
  177. {
  178. printf("Server: Could not create socket");
  179. }
  180. puts("Server: Socket created");
  181.  
  182. //Prepare the sockaddr_in structure
  183. server.sin_addr.s_addr = INADDR_ANY;
  184. server.sin_family = AF_INET;
  185. server.sin_port = htons(DEFAULT_PORT);
  186.  
  187. //Bind
  188. if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
  189. {
  190. //print the error message
  191. perror("Server: bind failed. Error");
  192. return 1;
  193. }
  194. puts("Server: bind done");
  195.  
  196. //Listen
  197. listen(socket_desc , 3);
  198.  
  199. //Accept and incoming connection
  200. puts("Server: Waiting for incoming connections...");
  201. c = sizeof(struct sockaddr_in);
  202.  
  203. //accept connection from an incoming client
  204. client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
  205.  
  206. if (client_sock < 0)
  207. {
  208. perror("Server: accept failed");
  209. return 1;
  210. }
  211.  
  212. puts("Server: Connection accepted");
  213.  
  214.  
  215.  
  216. char ime[DEFAULT_BUFLEN];
  217. char ime2[DEFAULT_BUFLEN] = "../";
  218. char *lista;
  219. int opcija;
  220.  
  221. recv(client_sock, &opcija, sizeof(opcija), 0);
  222.  
  223. int flagwrite = -1;
  224. int flagread = -1;
  225. FILE *fplist, *fp;
  226.  
  227. if(opcija == 1) {
  228. printf("Opcija: %d\n", opcija);
  229. read_size = recv(client_sock, ime, DEFAULT_BUFLEN, 0);
  230. ime[read_size] = '\0';
  231. printf("Ime: %s\n", ime);
  232. strcat(ime2, ime);
  233.  
  234. fplist = fopen("lista.txt", "r+");
  235. flagwrite = WriteList(fplist, ime);
  236. fclose(fplist);
  237. send(client_sock, &flagwrite, sizeof(flagwrite), 0);
  238. if(flagwrite == 0) {
  239.  
  240. fp = fopen(ime2, "wb");
  241. if(fp != NULL) {
  242. readfile(client_sock, fp);
  243. fclose(fp);
  244. }
  245. }
  246. }
  247. else if(opcija == 2) {
  248. printf("Opcija: %d\n", opcija);
  249. read_size = recv(client_sock, ime, DEFAULT_BUFLEN, 0);
  250. ime[read_size] = '\0';
  251. printf("Ime: %s\n", ime);
  252. strcat(ime2, ime);
  253.  
  254. fplist = fopen("lista.txt", "r");
  255. flagread = ReadList(fplist, ime);
  256. fclose(fplist);
  257. send(client_sock, &flagread, sizeof(flagread), 0);
  258. if(flagread == 0) {
  259.  
  260. fp = fopen(ime2, "rb");
  261. sendfile(client_sock, fp);
  262. fclose(fp);
  263. }
  264. }
  265. else if(opcija == 3) {
  266. fplist = fopen("lista.txt", "r");
  267. lista = List(fplist);
  268. send(client_sock, lista, strlen(lista), 0);
  269. fclose(fplist);
  270. }
  271.  
  272. close(socket_desc);
  273. close(client_sock);
  274.  
  275. return 0;
  276. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement