Advertisement
Guest User

Untitled

a guest
May 20th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.60 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <errno.h>
  4. #include <ctype.h>
  5. #include <string.h>
  6. #include <time.h>
  7. #include <unistd.h>
  8.  
  9. #include <sys/types.h>
  10. #include <sys/socket.h>
  11. #include <netdb.h>
  12. #include <arpa/inet.h>
  13. #include <netinet/in.h>
  14.  
  15. #include "../errlib
  16.  
  17.  
  18. #define MAX_File_number_to_request 5
  19. #define RequestCmd_length 1000
  20. #define BUFLEN 128 /* BUFFER LENGTH */
  21.  
  22. int main(int argc ,char * argv[])
  23. {
  24. //check arg number
  25. if(argc<=3)
  26. {
  27. fprintf(stderr,"missing parmeters. goy only %d . but expected at least 3",argc);
  28. }
  29.  
  30. //get port number
  31. int port =atoi(argv[2]);//Convert string to integer
  32.  
  33.  
  34.  
  35. //check if a vlaid ip address ofr not
  36. //struct hostent *Gethostbyname (const char *hostname);
  37. //gethostbyname() function returns a structure of type hostent for the given host name.
  38. //hostname--> is either a hostname or an IPv4 address in standard dot notation
  39. struct hostent *server;
  40. const char *hostname=argv[1];//???why index 1 . why not 0???
  41.  
  42. server =gethostbyname(hostname);
  43. if(server==NULL)
  44. {
  45. fprintf(stderr,"gethostbyname() failed for : %s \n",hostname);
  46. exit(1);
  47. }
  48.  
  49. //get ipaddress number
  50.  
  51. //#include <arpa/inet.h>
  52. //int inet_aton(const char *cp, struct in_addr *addr);
  53. //inet_aton()-convert Internet dot address to network address
  54.  
  55. struct in_addr serverIPaddr_struct; /* server IP addr. structure */
  56. int result = inet_aton(hostname, &serverIPaddr_struct);
  57. if (!result)
  58. {
  59. fprintf(stderr,"Invalid address.quitting \n");
  60. //err_quit("Invalid address");
  61. }
  62.  
  63.  
  64. //file names
  65. char files2Request[MAX_File_number_to_request];
  66. int n=argc-2;
  67.  
  68. if(n>MAX_File_number_to_request)
  69. fprintf(stderr,"only 50 file can be asked at a time. U asked for %d . so 1st %d i will be asked",n,MAX_File_number_to_request);
  70.  
  71.  
  72. for (int i=0;i<n;i++)
  73. {
  74. files2Request[i]=argv[i+2];
  75.  
  76.  
  77. printf("%s\n",files2Request[i]);
  78.  
  79.  
  80. }
  81. //construct request command
  82. char cmds[RequestCmd_length][500];
  83. //char * strcpy ( char * destination, const char * source );
  84. for ( i=0;i<n;i++)
  85. {
  86. bzero(cmds[i],RequestCmd_length);
  87. strcpy(cmds[i],"GET");//ASCII characters “GET”
  88. strcat(cmds[i]," ");// ASCII space character
  89. strcat(cmds[i],files2Request[i]);// ASCII characters of the file name
  90. strcat(cmds[i],"\r");//ASCII carriage return (CR)
  91. strcat(cmds[i],"\n");//ASCII line feed (LF)
  92.  
  93. fprintf(stderr,"CMD-%s : %s \n",cmd[i]);
  94. }
  95.  
  96. /* create the socket */
  97. printf("Creating socket\n");
  98. int sockfd = Socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  99. printf("done. Socket fd number: %d\n",sockfd);
  100.  
  101.  
  102.  
  103.  
  104. /* prepare address structure */
  105. struct sockaddr_in serv_addr;
  106. bzero(&serv_addr, sizeof(serv_addr));
  107. serv_addr.sin_family = AF_INET;
  108. serv_addr.sin_port = htons(port); //The htons() function converts the unsigned short integer hostshort from host byte order to network byte order.
  109. serv_addr.sin_addr = serverIPaddr_struct;
  110.  
  111.  
  112. /* connect */
  113. //showAddr("Connecting to target address", &serv_addr);
  114. printf("Connecting to target address\n");
  115. //Connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
  116. //printf("done.\n");
  117.  
  118. if(connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr))<0){
  119. error("Connection Failed");
  120. }
  121. else
  122. {
  123. printf("Client: Connecting...\n");
  124. }
  125.  
  126.  
  127. // a file descriptor (FD, less frequently fildes) is an abstract indicator (handle) used to access a file or other input/output resource, such as a pipe or network socket
  128.  
  129. //The write() function shall attempt to write nbyte bytes from the buffer pointed to by buf to the file associated with the open file descriptor, fildes
  130. Write(sockfd, cmds[0], strlen(buf));
  131.  
  132. /* reads exactly "n" bytes from a descriptor */
  133. //ssize_t readn (int fd, void *vptr, size_t n)
  134.  
  135. int indexCount;
  136. char receivedData[BUFLEN];
  137. //read 1st 3 character -->"+ O K"
  138. int character_read_count=readn(sockfd, receivedData, 3);///why 5 bit ??? why not 3 bit
  139.  
  140. //+ O K CR LF B1 B2 B3 B4 T1 T2 T3 T4 File content
  141. //read fist 3 character "+ O K"
  142. char *t= "+OK";
  143. int g=strlen(t);
  144. if( strncmp(receivedData,t,g) == 0 )
  145. {
  146. //read next 2 character. CR LF . useless . but we need to read increase the ptr
  147. character_read_count=readn(sockfd, receivedData, 2);
  148.  
  149.  
  150. //read next 4 bit. B1 B2 B3 B4
  151. uint32_t file_bytes;
  152. int n = readn(sockfd, &file_bytes, 4);
  153. file_bytes = ntohl(file_bytes);
  154. printf("file_bytes: %d\n",file_bytes);
  155.  
  156.  
  157.  
  158. //read next 4 bit. T1 T2 T3 T4
  159. uint32_t file_tm;
  160. n = readn(sockfd, &file_tm, 4);
  161. file_tm = ntohl(file_tm);
  162. printf("file_tm: %d\n",file_tm);
  163.  
  164.  
  165. //Writes an array of count elements, each one with a size of size bytes, from the block //of memory pointed by ptr to the current position in the stream.
  166. //size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream );
  167.  
  168. char *fileContent;
  169. fileContent=(char*)malloc(file_bytes* sizeof(char));
  170.  
  171. n = readn(sockfd, fileContent, file_bytes);
  172.  
  173. if(character_read_count != file_bytes)
  174. {
  175. printf("wrong number of byte received \n");
  176. }
  177. else
  178. {
  179.  
  180. char *fname;
  181. fname=files2Request[0];
  182. strcat(fname,"_Recieved");
  183.  
  184. FILE *fp;
  185. fp = fopen(fname, "w");
  186. if(NULL == fp)
  187. {
  188. printf("Error opening file");
  189. return 1;
  190. }
  191. else
  192. {
  193. fwrite(fileContent, sizeof(char), fileContent, fp);
  194. fclose(fp);
  195. }
  196. }
  197.  
  198.  
  199.  
  200. }
  201.  
  202. Write(sockfd, "QUIT/r/n", 6);
  203. close(sockfd);
  204.  
  205. printf("===========================================================\n");
  206.  
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement