Guest User

Untitled

a guest
Dec 22nd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.74 KB | None | 0 0
  1. //client
  2.  
  3. #include<stdio.h>
  4. #include<string.h>
  5. #include<sys/socket.h>
  6. #include<arpa/inet.h>
  7.  
  8. #define DEFAULT_BUFLEN 512
  9. #define DEFAULT_PORT 27015
  10. #define MAX_FILES_NAME 40
  11.  
  12. int sock, read_size;
  13. struct sockaddr_in server;
  14. char server_reply[DEFAULT_BUFLEN];
  15.  
  16. void login();
  17. void menu();
  18. int sendFile();
  19. int receiveFile();
  20. void delay(){
  21. long long i;
  22. for(i=0;i<99999;i++);
  23. }
  24. char success[] = "success!", fail[] = "failure!";
  25.  
  26. int main(int argc , char *argv[])
  27. {
  28.  
  29. login();
  30.  
  31. menu();
  32.  
  33. close(sock);
  34.  
  35. return 0;
  36. }
  37.  
  38. void login(){
  39. char user[DEFAULT_BUFLEN], password[DEFAULT_BUFLEN];
  40. //Create socket
  41. sock = socket(AF_INET , SOCK_STREAM , 0);
  42. if (sock == -1)
  43. {
  44. printf("Could not create socket");
  45. }
  46. puts("Socket created");
  47.  
  48. server.sin_addr.s_addr = inet_addr("127.0.0.1");
  49. server.sin_family = AF_INET;
  50. server.sin_port = htons( DEFAULT_PORT );
  51.  
  52. //Connect to remote server
  53. if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0)
  54. {
  55. perror("connect failed. Error");
  56. return;
  57. }
  58.  
  59. puts("Connected\n");
  60. while(1)
  61. {
  62. memset(server_reply,0,strlen(server_reply));
  63. printf("Enter username : ");
  64. scanf("%s" , user);
  65.  
  66. //slanje username-a serveru
  67. if( send(sock , user , DEFAULT_BUFLEN , 0) < 0)
  68. {
  69. puts("Send failed");
  70. return;
  71. }
  72.  
  73. printf("Enter password : ");
  74. scanf("%s" , password);
  75.  
  76. //slanje password-a serveru
  77. if( send(sock , password , DEFAULT_BUFLEN , 0) < 0)
  78. {
  79. puts("Send failed");
  80. return;
  81. }
  82.  
  83.  
  84. //prijem poruke od servera o uspesnosti prijavljivanja
  85. if( recv(sock , server_reply , DEFAULT_BUFLEN , 0) < 0)
  86. {
  87. puts("recv failed");
  88. break;
  89. }
  90.  
  91. puts("Server reply :");
  92. puts(server_reply);
  93.  
  94. if(strcmp(server_reply, "success!") == 0){
  95. return;
  96. }
  97. }
  98. }
  99.  
  100. void menu(){
  101. char message[DEFAULT_BUFLEN], iterations[DEFAULT_BUFLEN];
  102. int ind=0, i, j;
  103.  
  104. while(!ind){
  105. printf("\n1.upload\n2.download\n3.file list\n4.log off\n");
  106. printf("enter number: ");
  107. scanf("%s", message);
  108. if(message[1]!='\0'){
  109. printf("Enter one-digit number!\n");
  110. continue;
  111. }
  112.  
  113. if( send(sock , message , DEFAULT_BUFLEN , 0) < 0)
  114. {
  115. puts("Send failed");
  116. return;
  117. }
  118. switch(message[0]){
  119. case '1':
  120. if(sendFile()==1)
  121. continue;
  122. break;
  123. case '2':
  124. if(receiveFile()==1)
  125. continue;
  126. break;
  127. case '3':
  128. //prijem niza fajlova koji se trenutno nalaze na serveru
  129. if((recv(sock , iterations , DEFAULT_BUFLEN , 0)) < 0){
  130. puts("recv failed");
  131. return;
  132. }
  133. j=atoi(iterations);
  134. printf("Files on server: ");
  135. for(i=0;i<j;i++)
  136. {
  137. if((recv(sock , server_reply , DEFAULT_BUFLEN , 0)) < 0){
  138. puts("recv failed");
  139. return;
  140. }
  141. printf("%s", server_reply);
  142. memset(server_reply,0,strlen(server_reply));
  143. break;
  144. }
  145.  
  146. printf("\n");
  147. break;
  148. case '4':
  149. //prilikom odjave klijenta, izlazi se i beskonacne petlje i server se gasi
  150. ind = 1;
  151. break;
  152. default:
  153. printf("Enter number between 1 and 4!\n");
  154. }
  155. }
  156. printf("Client disconnected!\n");
  157. }
  158.  
  159. int sendFile(){
  160. char fileSize[DEFAULT_BUFLEN], filePath[DEFAULT_BUFLEN], chunk[1000], fileName[MAX_FILES_NAME];
  161. FILE *uploadFile;
  162. int size, i, n, percent;
  163. printf("Enter file name: ");
  164. scanf("%s", fileName);
  165. if(send(sock,fileName,MAX_FILES_NAME,0) < 0)
  166. {
  167. puts("Send failed");
  168. return 1;
  169. }
  170. memset(filePath, 0, DEFAULT_BUFLEN);
  171. strcpy(filePath, "./clientFiles/");
  172. strcat(filePath, fileName);
  173. printf("%s\n", filePath);
  174. if((uploadFile = fopen(filePath, "rb"))==NULL){
  175. perror("Opening upload file failed!\n");
  176. if(send(sock,fail,DEFAULT_BUFLEN,0) < 0)
  177. {
  178. puts("Send failed");
  179. return 1;
  180. }
  181. }
  182. //klijent obavestava server da je uspelo otvaranje i a moze da pocne razmena
  183. if(send(sock,success,DEFAULT_BUFLEN,0) < 0)
  184. {
  185. puts("Send failed");
  186. return 1;
  187. }
  188.  
  189. fseek(uploadFile, 0, SEEK_END);
  190. size = ftell(uploadFile);
  191. fseek(uploadFile, 0, SEEK_SET);
  192. n = size/1000;
  193. sprintf(fileSize, "%d", size);
  194. if(send(sock,fileSize,DEFAULT_BUFLEN,0) < 0)
  195. {
  196. puts("Send failed");
  197. return 1;
  198. }
  199. for(i = 0; i < n; i++){
  200. fread(chunk, sizeof(char), 1000, uploadFile);
  201. if(send(sock,chunk,1000,0) < 0)
  202. {
  203. puts("Send failed");
  204. return 1;
  205. }
  206. percent=i*100*1000/size;
  207. printf("\rUpload in progress:%d%%", percent);
  208. delay();
  209. }
  210. fread(chunk,sizeof(char),size%1000,uploadFile);
  211. if(send(sock,chunk,1000,0) < 0)
  212. {
  213. puts("Send failed");
  214. return 1;
  215. }
  216. printf("\rUpload in progress:100%%\n");
  217. printf("Upload successful\n");
  218.  
  219. if(!fclose(uploadFile)){
  220. printf("Closing upload file successful!\n");
  221. }else{
  222. printf("Closing upload file failed!\n");
  223. return 1;
  224. }
  225.  
  226. return 0;
  227. }
  228.  
  229. int receiveFile(){
  230. char fileOpen[DEFAULT_BUFLEN], fileName[MAX_FILES_NAME], filePath[DEFAULT_BUFLEN], buffer[1000], fileSize[DEFAULT_BUFLEN];
  231. int i, n, percent;
  232. FILE *downloadFile;
  233. printf("Enter file name: ");
  234. scanf("%s", fileName);
  235. if(send(sock,fileName,MAX_FILES_NAME,0) < 0)
  236. {
  237. puts("Send failed");
  238. return 1;
  239. }
  240.  
  241. if(recv(sock , fileOpen , DEFAULT_BUFLEN , 0) < 0){
  242. puts("recv failed");
  243. return 1;
  244. }
  245. //dobija informaciju od servera o uspesnosti otvaranja fajla
  246. if(strcmp(fileOpen, fail)==0){
  247. printf("Server could not open %s\n", fileName);
  248. return 1;
  249. }
  250. memset(filePath, 0, DEFAULT_BUFLEN);
  251. strcpy(filePath, "./clientFiles/");
  252. strcat(filePath, fileName);
  253. if((downloadFile = fopen(filePath, "wb"))==NULL){
  254. perror("Opening download file failed!\n");
  255. return 1;
  256. }
  257.  
  258. if(recv(sock , fileSize , DEFAULT_BUFLEN , 0) < 0){
  259. puts("recv failed");
  260. return 1;
  261. }
  262.  
  263. n = atoi(fileSize);
  264. for(i = 0; i<n/1000; i++){
  265. if(recv(sock , buffer , 1000 , 0) < 0){
  266. return 1;
  267. }
  268. fwrite(buffer, sizeof(char), 1000, downloadFile);
  269. percent=i*100*1000/n;
  270. printf("\rDownload in progress:%d%%", percent);
  271. delay();
  272. }
  273. if(recv(sock , buffer , 1000 , 0) < 0){
  274. return 1;
  275. }
  276. fwrite(buffer, sizeof(char), n%1000, downloadFile);
  277. printf("\rDownload in progress:100%%\n");
  278. printf("Download successful!\n");
  279. if(!fclose(downloadFile)){
  280. printf("Closing downloaded file successful!\n");
  281. }else{
  282. printf("Closing downloaded file failed!\n");
  283. return 1;
  284. }
  285.  
  286.  
  287. return 0;
  288. }
Add Comment
Please, Sign In to add comment