Advertisement
Guest User

Untitled

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