Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.72 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <sys/socket.h>
  6. #include <netinet/in.h>
  7. #include <arpa/inet.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10.  
  11. #define PORT htons(12221)
  12.  
  13. void ServeConnection(int sock)
  14. {
  15.     char path[512];
  16.     long dl_of_the_file, sent, sent_in_total, read;
  17.     struct stat fileinfo;
  18.     unsigned char buffer[1024];
  19.     FILE* file;
  20.    
  21.     printf("Give the path to the file: \n");
  22.     memset(path, 0, 512);
  23.     scanf("%s",path);
  24.    
  25.     if (stat(path, &fileinfo) < 0)
  26.     {
  27.         printf("Child: I can't obtain information about a file\n");
  28.         return;
  29.     }
  30.     if (fileinfo.st_size == 0)
  31.     {
  32.         printf("Child: filesize 0\n");
  33.         return;
  34.     }
  35.    
  36.     printf("Child: filesize : %d\n", fileinfo.st_size);
  37.    
  38.     dl_of_the_file = fileinfo.st_size;
  39.     sent_in_total = 0;
  40.     file = fopen(path, "rb");
  41.    
  42.     if (file == NULL)
  43.     {
  44.         printf("Child: error at opening a file\n");
  45.         return;
  46.     }
  47.    
  48.     while (sent_in_total < dl_of_the_file)
  49.     {
  50.         read = fread(buffer, 1, 1024, file);
  51.         sent = send(sock, buffer, read, 0);
  52.         if (read != sent)
  53.             break;
  54.         sent_in_total += sent;
  55.         printf("Child: total sent %ld\n", sent_in_total);
  56.     }
  57.    
  58.     if (sent_in_total == dl_of_the_file)
  59.     {
  60.         printf("Child: file sent ok\n");
  61.     }
  62.     else
  63.     {
  64.         printf("Child: error at sending a file\n");
  65.     }
  66.    
  67.     fclose(file);
  68.     return;    
  69. }
  70.    
  71.  
  72. int main()
  73. {
  74.     struct sockaddr_in adr;
  75.     socklen_t dladr = sizeof(struct sockaddr_in);
  76.     int sock_listen, sock_client;
  77.     sock_listen = socket(PF_INET, SOCK_STREAM, 0);
  78.     adr.sin_family = AF_INET;
  79.     adr.sin_port = PORT;
  80.     adr.sin_addr.s_addr = INADDR_ANY;
  81.     memset(adr.sin_zero, 0, sizeof(adr.sin_zero));
  82.    
  83.    
  84.     if (bind(sock_listen, (struct sockaddr*) &adr, dladr) < 0)
  85.     {
  86.         printf("Parent: bind failed\n");
  87.         return 1;
  88.     }
  89.    
  90.     listen(sock_listen, 10);
  91.     printf("Server running...\n");
  92.    
  93.     while(1)
  94.     {
  95.         dladr = sizeof(struct sockaddr_in);
  96.         sock_client = accept(sock_listen, (struct sockaddr*) &adr, &dladr);
  97.        
  98.         if (sock_client < 0)
  99.         {
  100.             printf("Parent: accept error\n");
  101.             continue;
  102.         }
  103.        
  104.         printf("Parent: connection from %s:%u\n",
  105.             inet_ntoa(adr.sin_addr),
  106.             ntohs(adr.sin_port));
  107.         printf("Parent: I'm creating child process\n");
  108.        
  109.    
  110.         if (fork() == 0)
  111.         {
  112.             //child
  113.             printf("Child: start serving)\n");
  114.             ServeConnection(sock_client);
  115.             printf("Child: closing socket\n");
  116.             close(sock_client);
  117.             printf("Child: end of the process\n");
  118.             exit(0);
  119.         }
  120.         else
  121.         {
  122.             //parent
  123.             printf("Parent: return to listening)\n");
  124.             continue;
  125.         }
  126.    
  127.     }
  128.     return 0;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement