Files
protohackersc/server06.c
2025-08-01 08:47:25 +01:00

43 lines
1.3 KiB
C

//
// Created by Ajurna on 01/08/2025.
//
#include "server06.h"
#include <winsock2.h>
#include <pthread.h>
#include "data.h"
#include <stdio.h>
int main() {
SOCKET server = get_listen_socket();
SOCKADDR_IN clientAddr;
SOCKET client;
int clientAddrSize = sizeof(clientAddr);
int connection_number = 1;
printf("Listening for incoming connections...\n");
while((client = accept(server, (SOCKADDR *)&clientAddr, &clientAddrSize)) != INVALID_SOCKET)
{
handle_args_t *args = malloc(sizeof(handle_args_t));
args->client = client;
args->connection = connection_number++;
pthread_t thread;
pthread_create(&thread, nullptr, handle_connection, args);
}
return 0;
}
void *handle_connection(void *args) {
handle_args_t *handleArgs = args;
char buffer[1024] = {0};
int bytesReceived;
connection_type_t connection_type = UNKNOWN;
byte_array_t *data = byte_array_create(1024);
while ((bytesReceived = recv(handleArgs->client, buffer, sizeof(buffer), 0)) > 0) {
printf("Client sent {%d}: |%d| \n", handleArgs->connection, bytesReceived);
byte_array_append(data, buffer, bytesReceived);
char *request;
while ((request = byte_array_get_bytes(data, 1)) != NULL) {
parse_request(handleArgs, request, data);
}
}
}