Server backup

 avatar
user_1944374
plain_text
a year ago
3.3 kB
0
Indexable
Never
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <ifaddrs.h>

typedef struct client_info_t {
    int client_socket;
    int thread_id;
} client_info_t;

void* handle_connection(void* arg) {
    client_info_t * client_info = (client_info_t*) arg;
    int client_socket = client_info->client_socket;
    char buffer[1024];
    int bytes_read;
    printf("Connected to new thread %lu\n", pthread_self());

    while(1) {
        // memset(buffer, 0, sizeof(buffer));
        // printf("Reading...\n");
        bytes_read = recv(client_socket, buffer, sizeof(buffer), 0);
        if(bytes_read == -1) {
            printf("Read failed\n");
            pthread_exit(NULL);
        }
        printf("Thread ID: %lu Recieved String: %s\n", pthread_self(), buffer);

        if(strcmp(buffer, "quit") == 0) {
            printf("Client disconnected from thread %lu\n", pthread_self());
            close(client_socket);
            client_info->client_socket = -1;
            pthread_exit(NULL);
        }
    }
}

int main() {
    client_info_t threads[5];
    pthread_t threadIds[5];
    for(int i = 0; i < 5; i++) {
        threads[i].client_socket = -1;
    }

    int server_socket, client_socket, addr_size;
    struct sockaddr_in address, client_addr;
    //creating the socket file descriptor
    memset(&address, 0, sizeof(address));
    address.sin_family = AF_INET;

    //accept IP address from the user
    printf("Enter the IP address you want: ");
    char ip[16];
    fgets(ip, 16, stdin);
    address.sin_addr.s_addr = inet_addr(ip);

    //accept port number from the user
    int port;
    printf("Enter the port number you want the server to bind to: ");
    scanf("%d", &port);
    address.sin_port = port;

    if((server_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        printf("Socket creation failed\n");
        exit(EXIT_FAILURE);
    }

    //bind and display IP address and port number
    if(bind(server_socket, (struct sockaddr*)&address, sizeof(address)) == -1) {
        printf("Bind failed\n");
        exit(EXIT_FAILURE);
    }
    else {
        printf("IP Address: %s", ip);
        printf("Port number: %d\n", port);
    }

    if(listen(server_socket, 5) == -1) {
        printf("Listen failed\n");
        exit(EXIT_FAILURE);
    }

    while(1) {
        addr_size = sizeof(struct sockaddr_in);
        if(client_socket = accept(server_socket, (struct sockaddr*)&client_addr, (socklen_t*)&addr_size) == -1) {
            printf("Accept failed\n");
        }
        
        int slot = -1;
        for(int i = 0; i < 5; i++) {
            if(threads[i].client_socket == -1) {
                slot = i;
                threads[i].client_socket = client_socket;
                threads[i].thread_id = slot;
                pthread_create(&threadIds[slot], NULL, handle_connection, &threads[slot]);
                break;
            }
        }

        if(slot == -1) {
            printf("Max client limit reached\n");
            close(client_socket);
        }
    }
    return 0;
}