server2 multithread

This commit is contained in:
2026-02-09 21:41:20 +01:00
parent cc842111ff
commit a5ecd9ff9d
6 changed files with 195 additions and 0 deletions

4
server2/.clangd Normal file
View File

@@ -0,0 +1,4 @@
CompileFlags:
Add:
- -Isrc
- -std=c11

69
server2/Makefile Normal file
View File

@@ -0,0 +1,69 @@
APPNAME = networkc
SRCDIR = src
BUILDDIR = build
VENDORDIR = vendor
target ?= linux
profile ?= debug
valgrind ?= false
ifeq ($(target), windows)
CC = x86_64-w64-mingw32-gcc
EXT = .exe
OPT_FLAGS = -O2 -DNDEBUG
INCLUDES = -I$(SRCDIR)
LIBS =
# Command to copy DLL after build
POST_BUILD_CMD = #@cp .. $(BUILDDIR); @echo "Build complete."
else
CC = gcc
EXT =
INCLUDES = -I$(SRCDIR)
LIBS = -lpthread
POST_BUILD_CMD = @echo "Build complete."
# Profile Logic
ifeq ($(profile), release)
OPT_FLAGS = -O3 -DNDEBUG -march=native
else
OPT_FLAGS = -O0 -g -DDEBUG -Wall -Wextra
endif
endif
# Combine Flags
CFLAGS = -Wall $(OPT_FLAGS) $(INCLUDES)
SRC = $(shell find $(SRCDIR) -name "*.c")
OBJ = $(SRC:$(SRCDIR)/%.c=$(BUILDDIR)/%.o)
all: $(BUILDDIR)/$(APPNAME)$(EXT)
$(BUILDDIR)/$(APPNAME)$(EXT): $(OBJ)
@echo "Linking $@ (Target: $(target), Profile: $(profile))"
@mkdir -p $(dir $@)
$(CC) $(OBJ) -o $@ $(LIBS)
$(POST_BUILD_CMD)
$(BUILDDIR)/%.o: $(SRCDIR)/%.c
@echo "Compiling $<"
@mkdir -p $(dir $@)
$(CC) $(CFLAGS) -c $< -o $@
clean:
rm -rf $(BUILDDIR)
ifeq ($(valgrind), true)
RUN_CMD = valgrind --leak-check=full ./$(BUILDDIR)/$(APPNAME)$(EXT)
else
RUN_CMD = ./$(BUILDDIR)/$(APPNAME)$(EXT)
endif
run: all
$(RUN_CMD)
.PHONY: all clean run

View File

@@ -0,0 +1,21 @@
[
{
"file": "src/main.c",
"arguments": [
"gcc",
"-Wall",
"-O0",
"-g",
"-DDEBUG",
"-Wall",
"-Wextra",
"-Isrc",
"-c",
"src/main.c",
"-o",
"build/main.o"
],
"directory": "/home/tom/Dev/networkc",
"output": "build/main.o"
}
]

101
server2/src/main.c Normal file
View File

@@ -0,0 +1,101 @@
#include <arpa/inet.h>
#include <netinet/in.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
void *connection_handler(void *);
int main() {
int socket_desc = socket(AF_INET, SOCK_STREAM, 0);
if (socket_desc == -1) {
printf("Could not create a socket!");
return 1;
}
struct sockaddr_in server;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_family = AF_INET;
server.sin_port = htons(8888);
// connect to remote server
if (bind(socket_desc, (struct sockaddr *)&server, sizeof(server)) < 0) {
puts("bind error\n");
return 1;
}
puts("bind done\n");
// listen for connections
listen(socket_desc, 3);
// accept incoming connections
puts("waiting for connections...");
int c = sizeof(struct sockaddr_in);
struct sockaddr_in client;
int new_socket, *new_sock;
while (new_socket = accept(socket_desc, (struct sockaddr *)&client, (socklen_t *)&c)) {
puts("connection accepted\n");
char *client_ip = inet_ntoa(client.sin_addr);
int client_port = ntohs(client.sin_port);
printf("client address: %s:%d\n", client_ip, client_port);
char *message = "Hello Client\n";
write(new_socket, message, strlen(message));
pthread_t sniffer_thread;
new_sock = malloc(1);
*new_sock = new_socket;
if (pthread_create(&sniffer_thread, NULL, connection_handler, (void *)new_sock) < 0) {
perror("could not create thread\n");
return 1;
}
// join the thread so we do not terminate before
// pthread_join(sniffer_thread, NULL);
puts("Handler assigned\n");
}
if (new_socket < 0) {
perror("accept failed\n");
return 1;
}
close(socket_desc);
return 0;
}
void *connection_handler(void *socket_desc) {
// socket descriptor
int sock = *(int *)socket_desc;
int read_size;
char *message = "I am the connection handler";
char client_message[2000];
// send some message to the client
write(sock, message, strlen(message));
// recieve message from the client
while ((read_size = recv(sock, client_message, 2000, 0)) > 0) {
// send the message back to the client
write(sock, client_message, strlen(client_message));
}
if (read_size == 0) {
puts("Client disconnected\n");
fflush(stdout);
} else if (read_size == -1) {
perror("recv failed\n");
}
free(socket_desc);
return 0;
}