Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.2 kB
2
Indexable
Never
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <pthread.h>

#define BUFFER_SIZE 1024
#define MAX_CLIENTS 5

void *client_handler(void *);

int main(int argc, char *argv[]) {
    int server_socket, client_socket, port;
    char buffer[BUFFER_SIZE];
    struct sockaddr_in server_addr, client_addr;
    socklen_t client_addr_len;
    pthread_t threads[MAX_CLIENTS];
    int thread_count = 0;

    if (argc < 2) {
        printf("Usage: %s <port>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    // create server socket
    server_socket = socket(AF_INET, SOCK_STREAM, 0);
    if (server_socket < 0) {
        perror("socket creation failed");
        exit(EXIT_FAILURE);
    }

    // set server address and port
    memset(&server_addr, 0, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = INADDR_ANY;
    port = atoi(argv[1]);
    server_addr.sin_port = htons(port);

    // bind server socket to address and port
    if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
        perror("bind failed");
        exit(EXIT_FAILURE);
    }

    // listen for incoming connections
    if (listen(server_socket, MAX_CLIENTS) < 0) {
        perror("listen failed");
        exit(EXIT_FAILURE);
    }

    // print server address and port
    printf("Server listening on %s:%d\n", inet_ntoa(server_addr.sin_addr), ntohs(server_addr.sin_port));

    // accept incoming connections and spawn client threads
    while (1) {
        client_addr_len = sizeof(client_addr);
        client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &client_addr_len);
        if (client_socket < 0) {
            perror("accept failed");
            break;
        }
        printf("Client connected from %s:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
        if (pthread_create(&threads[thread_count], NULL, client_handler, (void *)&client_socket) != 0) {
            perror("thread creation failed");
            break;
        }
        thread_count++;
        if (thread_count >= MAX_CLIENTS) {
            printf("Maximum number of clients reached\n");
            break;
        }
    }

    // join client threads and exit
    for (int i = 0; i < thread_count; i++) {
        pthread_join(threads[i], NULL);
    }
    close(server_socket);
    exit(EXIT_SUCCESS);
}

void *client_handler(void *arg) {
    int client_socket = *(int *)arg;
    char buffer[BUFFER_SIZE];
    pthread_t self = pthread_self();

    // receive and print client strings until client disconnects or types "quit"
    while (1) {
        memset(buffer, 0, sizeof(buffer));
        if (recv(client_socket, buffer, BUFFER_SIZE, 0) < 0) {
            perror("recv failed");
            break;
        }
        printf("Thread %lu received string: %s", self, buffer);
        if (strcmp(buffer, "quit\n") == 0) {
            break;
        }
    }

    // close client socket and exit thread
    printf("Thread %lu exiting\n", self);