Advertisement
Guest User

receive.c

a guest
Apr 23rd, 2025
75
0
341 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.62 KB | Source Code | 0 0
  1. #include <arpa/inet.h>
  2. #include <netinet/in.h>
  3. #include <stdint.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <unistd.h>
  8.  
  9. int main() {
  10.  
  11.   const int PORT = 8080;
  12.   const int BUFFER_SIZE = 1024;
  13.  
  14.   int client_socket;
  15.   struct sockaddr_in server_addr, client_addr;
  16.   char buffer[BUFFER_SIZE];
  17.   socklen_t addr_len = sizeof(client_addr);
  18.  
  19.   int sock = socket(AF_INET, SOCK_STREAM, 0);
  20.   if (sock < 0) {
  21.     perror("Socket failed");
  22.     exit(EXIT_FAILURE);
  23.   }
  24.  
  25.   server_addr.sin_family = AF_INET;
  26.   server_addr.sin_addr.s_addr = INADDR_ANY;
  27.   server_addr.sin_port = htons(PORT);
  28.   if (bind(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
  29.     perror("Bind failed");
  30.     exit(EXIT_FAILURE);
  31.   }
  32.  
  33.   if (listen(sock, 1) < 0) {
  34.     perror("Listening failed");
  35.     exit(EXIT_FAILURE);
  36.   }
  37.   printf("Server listening on port %d...\n", PORT);
  38.  
  39.   client_socket = accept(sock, (struct sockaddr *)&client_addr, &addr_len);
  40.   if (client_socket < 0) {
  41.     perror("Accept failed");
  42.     exit(EXIT_FAILURE);
  43.   }
  44.   printf("Client connected.\n");
  45.  
  46.   uint32_t argc;
  47.   if (read(client_socket, &argc, sizeof(argc)) < 0) {
  48.     perror("Failed to read argc");
  49.     close(client_socket);
  50.     close(sock);
  51.     exit(EXIT_FAILURE);
  52.   }
  53.   argc = ntohl(argc);
  54.   printf("%zu file(s) will be received.\n", (unsigned long)argc);
  55.  
  56.   char title_list[argc][BUFFER_SIZE];
  57.   for (int i = 0; i < argc;) {
  58.     if (read(client_socket, title_list[i], BUFFER_SIZE) <= 0) {
  59.       perror("Failed to read file name");
  60.       close(client_socket);
  61.       close(sock);
  62.       exit(EXIT_FAILURE);
  63.     }
  64.     if (title_list[i][0]) {
  65.       i++;
  66.     }
  67.   }
  68.  
  69.   for (int i = 0; i < argc; i++) {
  70.     FILE *f = fopen(title_list[i], "wb");
  71.     printf("Receiving file %d: %s\n", i + 1, title_list[i]);
  72.     if (f == NULL) {
  73.       perror("Error opening file for writing");
  74.       close(client_socket);
  75.       close(sock);
  76.       exit(EXIT_FAILURE);
  77.     }
  78.     while (1) {
  79.       memset(buffer, 0, BUFFER_SIZE);
  80.       ssize_t bytes_read = read(client_socket, buffer, BUFFER_SIZE);
  81.       if (bytes_read < 0) {
  82.         perror("Error reading from client socket");
  83.         fclose(f);
  84.         close(client_socket);
  85.         close(sock);
  86.         exit(EXIT_FAILURE);
  87.       }
  88.       if (bytes_read == 0) {
  89.         break;
  90.       }
  91.       if (fwrite(buffer, sizeof(char), bytes_read, f) != bytes_read) {
  92.         perror("Error writing to file");
  93.         fclose(f);
  94.         close(client_socket);
  95.         close(sock);
  96.         exit(EXIT_FAILURE);
  97.       }
  98.     }
  99.     fclose(f);
  100.   }
  101.   close(client_socket);
  102.   close(sock);
  103.   return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement