server06 start
This commit is contained in:
43
server06.c
Normal file
43
server06.c
Normal file
@@ -0,0 +1,43 @@
|
||||
//
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user