VastMind

Untitled

Jun 19th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <strings.h>
  5. #include <sys/socket.h>
  6. #include <arpa/inet.h>
  7. #include <sys/stat.h>
  8. #include <sys/time.h>
  9. #include <fcntl.h>
  10. #include <string.h>
  11. /* Size of the buffer used to send the file in several blocks */
  12. #define BUFFERT 512
  13.  
  14. static int create_server_socket(int port, in_addr_t ip );
  15. static struct sockaddr_in sock_serv;
  16. /**
  17. * @brief Function which allowing the creation of a socket returns a file descriptor
  18. * @param port port of the socket
  19. * @param ipaddr  ip address of the socket
  20. * @return soket's file descriptor value.
  21. */
  22. static int
  23. create_client_socket(int port, in_addr_t ip )
  24. {
  25.     int l;
  26.     int sfd;
  27.     int yes = 1;
  28.  
  29.     sfd = socket(PF_INET, SOCK_STREAM, 0);
  30.  
  31.     if (sfd == -1)
  32.     {
  33.         perror("Socket failure");
  34.         return EXIT_SUCCESS;
  35.     }
  36.  
  37.     if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
  38.     {
  39.         perror("Setsockopt error");
  40.         exit(5);
  41.     }
  42.  
  43.     /* Preparing the address of the destination socket */
  44.     l = sizeof(struct sockaddr_in);
  45.     struct sockaddr_in sock_serv, sock_clt;
  46.     /* @ToDo non-POSIX func must be replaced to the memset()
  47.      * memset( ):
  48.      */
  49.     bzero(&sock_serv, l);
  50.  
  51.     sock_serv.sin_family      = AF_INET;
  52.     sock_serv.sin_port        = htons(port);
  53.     sock_serv.sin_addr.s_addr = ip;
  54.  
  55.     /* Assign an identity to the socket */
  56.     if (bind(sfd, (struct sockaddr *) &sock_serv, l) == -1)
  57.     {
  58.         perror("Fail the binding");
  59.         return EXIT_FAILURE;
  60.     }
  61.  
  62.     struct in_addr addr;
  63.     memset(&addr, 0,  sizeof (struct in_addr));
  64.     addr.s_addr = ip;
  65.     printf("Client started and listening on %s:%d\n", inet_ntoa (addr), port );
  66.  
  67.     return sfd;
  68. }
  69.  
  70. int main(int argc, char **argv) {
  71.     /* Connection variables */
  72.     in_addr_t    ip_p = 0x7f000001; /* Default ip address is 127.0.0.1 */
  73.     int          port_p = 1234;
  74.     char         ch; /* char for the parameter to read from argv */
  75.     int sfd, fd; /* Descriptors */
  76.     char buf[BUFFERT];
  77.     int l = sizeof(struct sockaddr_in);
  78.     off_t count = 0, m, sz;
  79.     long int n;
  80.     struct stat buffer;
  81. //    if (argc != 4)
  82. //    {
  83. //        printf("Error usage : %s <ip_server> <port_server> <filename>\n", argv[0]);
  84. //        return EXIT_FAILURE;
  85. //    }
  86.     /* Parsing the command line parameters and arguments */
  87.     while ((ch = getopt(argc, argv, "d:p:f:")) != EOF)
  88.         switch (ch)
  89.         {
  90.             case 'd':
  91.                 ip_p = inet_addr(optarg);
  92.                 break;
  93.             case 'p':
  94.                 port_p = atoi(optarg);
  95.                 break;
  96.             case 'u':
  97.                 printf("updating the server");
  98.                 break;
  99.             default:
  100.                 fprintf(stderr, "Unknown option: '%s'\n", optarg);
  101.                 return 1;
  102.         }
  103.     /* Creating the socket file descriptor */
  104.  
  105.     sfd = create_client_socket(port_p, ip_p);
  106.  
  107.     if ((fd = open(argv[3], O_RDONLY)) == -1)
  108.     {
  109.         perror("Socket opening failure");
  110.         return EXIT_FAILURE;
  111.     }
  112.     /* file size */
  113.     if (stat(argv[3], &buffer) == -1)
  114.     {
  115.         perror("Stat fail");
  116.         return EXIT_FAILURE;
  117.     } else
  118.         sz = buffer.st_size;
  119.     // preparation of the transmission
  120.     bzero(&buf, BUFFERT);
  121.     if (connect(sfd, (struct sockaddr *) &sock_serv, l) == -1)
  122.     {
  123.         perror("Connection error\n");
  124.         exit(3);
  125.     }
  126.  
  127.     n = sprintf(buf,"File %s\n",argv[3]);
  128.     m = sendto(sfd, buf, n, 0, (struct sockaddr *) &sock_serv, l);
  129.     if (m == -1)
  130.     {
  131.         perror("File sending error");
  132.         return EXIT_FAILURE;
  133.     }
  134.     n = sprintf(buf,"%ld\n",sz);
  135.     m = sendto(sfd, buf, n, 0, (struct sockaddr *) &sock_serv, l);
  136.     if (m == -1)
  137.     {
  138.         perror("File sending error");
  139.         return EXIT_FAILURE;
  140.     }
  141.  
  142.     n = read(fd, buf, BUFFERT);
  143.     while (n)
  144.     {
  145.         if (n == -1)
  146.         {
  147.             perror("Reading fails");
  148.             return EXIT_FAILURE;
  149.         }
  150.         m = sendto(sfd, buf, n, 0, (struct sockaddr *) &sock_serv, l);
  151.         if (m == -1) {
  152.             perror("File sending error");
  153.             return EXIT_FAILURE;
  154.         }
  155.         count += m;
  156.         bzero(buf, BUFFERT);
  157.         n = read(fd, buf, BUFFERT);
  158.     }
  159.     /* Sending data to the server */
  160.     m = sendto(sfd, buf, 0, 0, (struct sockaddr *) &sock_serv, l);
  161.     while (n = recv(sfd, buf, BUFFERT, 0))
  162.     {
  163.         printf("%s",buf);
  164.     }
  165.  
  166.     printf("Bytes transferred:% ld \n", count);
  167.     printf("Total size:% ld \n", sz);
  168.     close(sfd);
  169.     return EXIT_SUCCESS;
  170. }
Add Comment
Please, Sign In to add comment