Facebook
From Rosidul, 5 Months ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 258
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <arpa/inet.h>
  6. #include <pthread.h>
  7.  
  8. #define PORT 12345
  9. #define BUFFER_SIZE 1024
  10. #define MAX_CLIENTS 5
  11.  
  12. // Structure to hold client information
  13. struct ClientInfo {
  14.     int socket;
  15.     struct sockaddr_in address;
  16. };
  17.  
  18. // Function to handle each client
  19. void *handleClient(void *arg) {
  20.     struct ClientInfo *clientInfo = (struct ClientInfo *)arg;
  21.     int clientSocket = clientInfo->socket;
  22.  
  23.     // Open and send file
  24.     FILE *fileToSend = fopen&#40;"file_to_send.txt", "rb"&#41;;
  25.     if (!fileToSend) {
  26.         perror("Error opening file");
  27.         close(clientSocket);
  28.         free(clientInfo);
  29.         pthread_exit(NULL);
  30.     }
  31.  
  32.     char buffer[BUFFER_SIZE];
  33.  
  34.     while (1) {
  35.         int bytesRead = fread(buffer, 1, sizeof(buffer), fileToSend);
  36.         if (bytesRead <= 0) {
  37.             break; // End of file or error
  38.         }
  39.         send(clientSocket, buffer, bytesRead, 0);
  40.     }
  41.  
  42.     printf("File sent to %s:%dn", inet_ntoa(clientInfo->address.sin_addr), ntohs(clientInfo->address.sin_port));
  43.  
  44.     // Close the socket and file
  45.     fclose(fileToSend);
  46.     close(clientSocket);
  47.     free(clientInfo);
  48.     pthread_exit(NULL);
  49. }
  50.  
  51. int main() {
  52.     int serverSocket, clientSocket;
  53.     struct sockaddr_in serverAddr, clientAddr;
  54.     socklen_t addrSize = sizeof(struct sockaddr_in);
  55.  
  56.     // Create socket
  57.     if ((serverSocket = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
  58.         perror("Error creating socket");
  59.         exit(EXIT_FAILURE);
  60.     }
  61.  
  62.     // Configure server address
  63.     memset(&serverAddr;, 0, sizeof(serverAddr));
  64.     serverAddr.sin_family = AF_INET;
  65.     serverAddr.sin_port = htons(PORT);
  66.     serverAddr.sin_addr.s_addr = INADDR_ANY;
  67.  
  68.     // Bind the socket
  69.     if (bind(serverSocket, (struct sockaddr *)&serverAddr;, sizeof(serverAddr)) == -1) {
  70.         perror("Error binding socket");
  71.         close(serverSocket);
  72.         exit(EXIT_FAILURE);
  73.     }
  74.  
  75.     // Listen for incoming connections
  76.     if (listen(serverSocket, MAX_CLIENTS) == -1) {
  77.         perror("Error listening for connections");
  78.         close(serverSocket);
  79.         exit(EXIT_FAILURE);
  80.     }
  81.  
  82.     printf("Server listening on port %d...n", PORT);
  83.  
  84.     // Array to store thread IDs
  85.     pthread_t threadIDs[MAX_CLIENTS];
  86.     int clientCount = 0;
  87.  
  88.     while (1) {
  89.         // Accept a connection
  90.         if ((clientSocket = accept(serverSocket, (struct sockaddr *)&clientAddr;, &addrSize;)) == -1) {
  91.             perror("Error accepting connection");
  92.             close(serverSocket);
  93.             exit(EXIT_FAILURE);
  94.         }
  95.  
  96.         // Create a new thread to handle the client
  97.         struct ClientInfo *clientInfo = (struct ClientInfo *)malloc(sizeof(struct ClientInfo));
  98.         clientInfo->socket = clientSocket;
  99.         clientInfo->address = clientAddr;
  100.  
  101.         if (pthread_create(&threadIDs;[clientCount], NULL, handleClient, (void *)clientInfo) != 0) {
  102.             perror("Error creating thread");
  103.             close(clientSocket);
  104.             free(clientInfo);
  105.         }
  106.  
  107.         clientCount++;
  108.  
  109.         // Check if the maximum number of clients is reached
  110.         if (clientCount >= MAX_CLIENTS) {
  111.             printf("Maximum number of clients reached. Closing the server.n");
  112.             break;
  113.         }
  114.     }
  115.  
  116.     // Join all threads before exiting
  117.     for (int i = 0; i < clientCount; i++) {
  118.         pthread_join(threadIDs[i], NULL);
  119.     }
  120.  
  121.     // Close the server socket
  122.     close(serverSocket);
  123.  
  124.     return 0;
  125. }
  126.  

Replies to Untitled rss

Title Name Language When
Re: Untitled Rosidul text 5 Months ago.