Facebook
From Rosidul, 1 Year ago, written in Plain Text.
This paste is a reply to Untitled from Rosidul - go back
Embed
Viewing differences between Untitled and Re: Untitled
#include 
#include 
#include 
#include h>
#include h>
#include 

#define PORT 12345
#define BUFFER_SIZE 1024
#define MAX_CLIENTS 5

// Structure to hold client information
struct ClientInfo {
    int SOCKET socket;
    struct sockaddr_in address;
};

// Function to handle each client
void *handleClient(void *arg) DWORD WINAPI handleClient(LPVOID lpParam) {
    struct ClientInfo *clientInfo = (struct ClientInfo *)arg;
    int 
*)lpParam;
    SOCKET 
clientSocket = clientInfo->socket;

    // Open and send file
    FILE *fileToSend = fopen("file_to_send.txt", "rb");
    if (!fileToSend) {
        perror("Error printf("Error opening file");
        close(clientSocket);
file\n");
        closesocket(clientSocket);
        free(clientInfo);
        pthread_exit(NULL);
return 1;
    }

    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", %s:%d\n", inet_ntoa(clientInfo->address.sin_addr), ntohs(clientInfo->address.sin_port));

    // Close the socket and file
    fclose(fileToSend);
    close(clientSocket);
closesocket(clientSocket);
    free(clientInfo);
    pthread_exit(NULL);
return 0;
}

int main() {
    int WSADATA wsaData;
    SOCKET 
serverSocket, clientSocket;
    struct sockaddr_in serverAddr, clientAddr;
    socklen_t int addrSize = sizeof(struct sockaddr_in);

sockaddr_in);

    // Initialize Winsock
    if (WSAStartup(MAKEWORD(2, 2), &wsaData;) != 0) {
        printf("WSAStartup failed\n");
        return 1;
    }

    // Create socket
    if ((serverSocket = socket(AF_INET, SOCK_STREAM, 0)) == -1) INVALID_SOCKET) {
        perror("Error printf("Error creating socket");
        exit(EXIT_FAILURE);
socket: %d\n", WSAGetLastError());
        WSACleanup();
        return 1;
    }

    // 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) SOCKET_ERROR) {
        perror("Error printf("Error binding socket");
        close(serverSocket);
        exit(EXIT_FAILURE);
socket: %d\n", WSAGetLastError());
        closesocket(serverSocket);
        WSACleanup();
        return 1;
    }

    // Listen for incoming connections
    if (listen(serverSocket, MAX_CLIENTS) == -1) SOCKET_ERROR) {
        perror("Error printf("Error listening for connections");
        close(serverSocket);
        exit(EXIT_FAILURE);
connections: %d\n", WSAGetLastError());
        closesocket(serverSocket);
        WSACleanup();
        return 1;
    }

    printf("Server listening on port %d...n", PORT);

    // Array to store thread IDs
    pthread_t threadIDs[MAX_CLIENTS];
    int clientCount = 0;

\n", PORT);

    while (1) {
        // Accept a connection
        if ((clientSocket = accept(serverSocket, (struct sockaddr *)&clientAddr;, &addrSize;)) == -1) INVALID_SOCKET) {
            perror("Error printf("Error accepting connection");
            close(serverSocket);
            exit(EXIT_FAILURE);
connection: %d\n", WSAGetLastError());
            closesocket(serverSocket);
            WSACleanup();
            return 1;
        }

        // 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, HANDLE hThread = CreateThread(NULL, 0, handleClient, (void *)clientInfo) != 0) (LPVOID)clientInfo, 0, NULL);
        if (hThread == NULL) 
{
            perror("Error printf("Error creating thread");
            close(clientSocket);
thread\n");
            closesocket(clientSocket);
            free(clientInfo);
        }

        clientCount++;

        // Check if the maximum number of clients is reached
        if (clientCount >= MAX_CLIENTS) 
} else {
            printf("Maximum number of clients reached. Closing the server.n");
            break;
CloseHandle(hThread);
        }
    }\n\n    // Join all threads before exiting\n    for (int i = 0; i < clientCount; i++) {\n        pthread_join(threadIDs[i], NULL);\n    }

    // Close the server socket
    close(serverSocket);

closesocket(serverSocket);
    WSACleanup();

    return 0;
}

Replies to Re: Untitled rss

Title Name Language When
Re: Re: Untitled Rosidul text 1 Year ago.