Advertisement
Guest User

U_CLIENT

a guest
Mar 14th, 2013
992
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.79 KB | None | 0 0
  1. /*
  2.  * uclient.c
  3.  *
  4.  *  Created on: Mar 14, 2013
  5.  *      Author: none
  6.  */
  7.  
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <unistd.h>
  13. #include <fcntl.h>
  14. #include <sys/socket.h>
  15. #include <sys/types.h>
  16. #include <arpa/inet.h>
  17. #include <netinet/in.h>
  18. #include <errno.h>
  19.  
  20. #define PORT        3666
  21.  
  22. void    addr_init(struct sockaddr_in *addr, int port, const char *IP){
  23.     addr->sin_family    = AF_INET;
  24.     addr->sin_port      = htons(port);
  25.     inet_pton(AF_INET, IP, (void*)&addr->sin_addr);
  26. }
  27.  
  28. void die(const char *type_error){
  29.     printf("Error \"%s\" in %s\n", strerror(errno), type_error);
  30.     exit(1);        //Remember: exit doesn't terminate current function but the entire program (closing and saving each file descriptor).
  31. }
  32.  
  33. int main (int argc, char **argv){
  34.  
  35.     /*
  36.      * Create principal socket members
  37.      */
  38.     int                 client_fd;
  39.     struct sockaddr_in  server;
  40.     socklen_t           addr_size = sizeof(struct sockaddr);
  41.  
  42.     char                buff_to_read[64];   //buff you'll use to read from server
  43.     char                *file_name;         //a pointer to argv[2], your file name
  44.  
  45.     size_t              size_to_read = 0, size_read = 0;
  46.  
  47.     if (argc < 3){
  48.         printf("Usage: <%s><IP-dest><FILE name>\n", argv[0]);
  49.         exit(1);
  50.     }
  51.  
  52.     if ( (client_fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0){
  53.         die("socket()");
  54.     }
  55.     puts("socket()");
  56.  
  57.     server.sin_family = AF_INET;
  58.     server.sin_port = htons(PORT);
  59.     server.sin_addr.s_addr = inet_addr(argv[1]);
  60.     file_name = argv[2];
  61.  
  62.     //SEND TO SERVER YOUR FILE NAME
  63.     if ( (sendto(client_fd, file_name, strlen(file_name), 0, (struct sockaddr*)&server, addr_size)) < 0 ){
  64.         printf("Error \"%s\" in sendto()\n", strerror(errno));
  65.         exit(1);
  66.     }
  67.     puts("file name sent");
  68.  
  69.     //RECV FROM SERVER YOUR FILE SIZE
  70.     if ( (recvfrom(client_fd, (size_t*)&size_to_read, sizeof(size_to_read), 0, NULL, NULL)) < 0){
  71.         printf("Error \"%s\" in recvfrom()\n", strerror(errno));
  72.         exit(1);
  73.     }
  74.     printf("Size of file: %lu\n", size_to_read);
  75.  
  76.     //IF FILE SIZE < 0: ERROR, EXIT(1)
  77.     if (size_to_read < 0){
  78.         printf("size_to_read < 0\n");
  79.         exit(1);
  80.     }
  81.    
  82.     //This commented region doesn't work, but meanwhile I'll try to fix it.
  83.     /*
  84.      *
  85.     //READ UNTIL YOUR FILE SIZE IS > 0
  86.     //size_to_read  = length of the message that is going to be sent from server
  87.     //size_read     = length of each message received from server on each iteration
  88.     while (size_to_read > 0){
  89.         if ( (size_read = recvfrom(client_fd, buff_to_read, sizeof(buff_to_read), 0, NULL, NULL)) < 0){
  90.             printf("Error \"%s\" in recvfrom()\n", strerror(errno));
  91.             exit(1);
  92.         }
  93.  
  94.         size_to_read -= size_read;
  95.     }*/
  96.    
  97.     if ( (size_read = recvfrom(client_fd, buff_to_read, sizeof(buff_to_read), 0, NULL, NULL)) < 0){
  98.         printf("Error \"%s\" in recvfrom()\n", strerror(errno));
  99.         exit(1);
  100.     }
  101.  
  102.     puts("file received");
  103.  
  104.     puts(buff_to_read);
  105.  
  106.     close(client_fd);
  107.     return EXIT_SUCCESS;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement