- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <arpa/inet.h>
- #include <pthread.h>
- #define PORT 12345
- #define BUFFER_SIZE 1024
- #define MAX_CLIENTS 5
- // Structure to hold client information
- struct ClientInfo {
- int socket;
- struct sockaddr_in address;
- };
- // Function to handle each client
- void *handleClient(void *arg) {
- struct ClientInfo *clientInfo = (struct ClientInfo *)arg;
- int clientSocket = clientInfo->socket;
- // Open and send file
- FILE *fileToSend = fopen("file_to_send.txt", "rb");
- if (!fileToSend) {
- perror("Error opening file");
- close(clientSocket);
- free(clientInfo);
- pthread_exit(NULL);
- }
- char buffer[BUFFER_SIZE];
- while (1) {
- int bytesRead = fread(buffer, 1, sizeof(buffer), fileToSend);
- if (bytesRead <= 0) {
- break; // End of file or error
- }
- send(clientSocket, buffer, bytesRead, 0);
- }
- printf("File sent to %s:%dn", inet_ntoa(clientInfo->address.sin_addr), ntohs(clientInfo->address.sin_port));
- // Close the socket and file
- fclose(fileToSend);
- close(clientSocket);
- free(clientInfo);
- pthread_exit(NULL);
- }
- int main() {
- int serverSocket, clientSocket;
- struct sockaddr_in serverAddr, clientAddr;
- socklen_t addrSize = sizeof(struct sockaddr_in);
- // Create socket
- if ((serverSocket = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
- perror("Error creating socket");
- exit(EXIT_FAILURE);
- }
- // Configure server address
- memset(&serverAddr;, 0, sizeof(serverAddr));
- serverAddr.sin_family = AF_INET;
- serverAddr.sin_port = htons(PORT);
- serverAddr.sin_addr.s_addr = INADDR_ANY;
- // Bind the socket
- if (bind(serverSocket, (struct sockaddr *)&serverAddr;, sizeof(serverAddr)) == -1) {
- perror("Error binding socket");
- close(serverSocket);
- exit(EXIT_FAILURE);
- }
- // Listen for incoming connections
- if (listen(serverSocket, MAX_CLIENTS) == -1) {
- perror("Error listening for connections");
- close(serverSocket);
- exit(EXIT_FAILURE);
- }
- printf("Server listening on port %d...n", PORT);
- // Array to store thread IDs
- pthread_t threadIDs[MAX_CLIENTS];
- int clientCount = 0;
- while (1) {
- // Accept a connection
- if ((clientSocket = accept(serverSocket, (struct sockaddr *)&clientAddr;, &addrSize;)) == -1) {
- perror("Error accepting connection");
- close(serverSocket);
- exit(EXIT_FAILURE);
- }
- // Create a new thread to handle the client
- struct ClientInfo *clientInfo = (struct ClientInfo *)malloc(sizeof(struct ClientInfo));
- clientInfo->socket = clientSocket;
- clientInfo->address = clientAddr;
- if (pthread_create(&threadIDs;[clientCount], NULL, handleClient, (void *)clientInfo) != 0) {
- perror("Error creating thread");
- close(clientSocket);
- free(clientInfo);
- }
- clientCount++;
- // Check if the maximum number of clients is reached
- if (clientCount >= MAX_CLIENTS) {
- printf("Maximum number of clients reached. Closing the server.n");
- break;
- }
- }
- // Join all threads before exiting
- for (int i = 0; i < clientCount; i++) {
- pthread_join(threadIDs[i], NULL);
- }
- // Close the server socket
- close(serverSocket);
- return 0;
- }