diff --git a/backend/GNUmakefile b/backend/GNUmakefile new file mode 100644 index 0000000..c7a87c5 --- /dev/null +++ b/backend/GNUmakefile @@ -0,0 +1,19 @@ + +CFLAGS = -std=c17 -Wall -Wextra -Wpedantic -Wconversion + +HEADERS = $(wildcard *.h) + +all: compile_flags.txt server + +server: main.o linux.o + gcc $^ -o $@ + +%.o: %.c $(HEADERS) + gcc $< -c -o $@ $(CFLAGS) + +clean: + rm -rf *.o server client + +compile_flags.txt: + echo -xc $(CFLAGS) | sed 's/\s\+/\n/g' > compile_flags.txt + diff --git a/backend/Makefile b/backend/Makefile index 1e1df18..5a26094 100644 --- a/backend/Makefile +++ b/backend/Makefile @@ -1,19 +1,12 @@ -CFLAGS = -std=c17 -Wall -Wextra -Wpedantic -Wconversion +OBJS=main.obj windows.obj -HEADERS = $(wildcard *.h) +all: $(OBJS) + link /out:server.exe $(OBJS) WS2_32.lib -all: compile_flags.txt server - -server: main.o - gcc $^ -o $@ - -%.o: %.c $(HEADERS) - gcc $< -c -o $@ $(CFLAGS) +.obj: + cl $*.c clean: - rm -rf *.o server client - -compile_flags.txt: - echo -xc $(CFLAGS) | sed 's/\s\+/\n/g' > compile_flags.txt + del *.obj server.exe diff --git a/backend/linux.c b/backend/linux.c new file mode 100644 index 0000000..d5a89ad --- /dev/null +++ b/backend/linux.c @@ -0,0 +1,11 @@ +#include "native.h" + +void init_socket(void) +{ +} + +void close_socket(int socket) +{ + close(socket); +} + diff --git a/backend/main.c b/backend/main.c index 99e57ac..b908f69 100644 --- a/backend/main.c +++ b/backend/main.c @@ -1,12 +1,10 @@ -#include -#include #include #include #include #include #include -#include -#include + +#include "native.h" typedef enum { HttpMethodGet, @@ -47,6 +45,8 @@ const uint16_t port = 8000; int main(void) { + init_sockets(); + printf("starting server...\n"); int server_socket = socket(AF_INET, SOCK_STREAM, 0); if (server_socket < 0) { @@ -95,13 +95,13 @@ int main(void) "hej med " "dig

fuck c hashtag

\r\n" "\r\n"; - ssize_t written = write(client_socket, send_buffer, sizeof(send_buffer)); + ssize_t written = send(client_socket, send_buffer, sizeof(send_buffer), 0); if (written < 0) { printf("error: could not write\n"); continue; } printf("disconnecting client\n"); - close(client_socket); + close_socket(client_socket); } } diff --git a/backend/native.h b/backend/native.h new file mode 100644 index 0000000..b1d0e2d --- /dev/null +++ b/backend/native.h @@ -0,0 +1,16 @@ +#ifdef _WIN32 + #include + #include + #include + typedef SSIZE_T ssize_t; +#else + #include + #include + #include + #include +#endif + +void init_sockets(void); + +void close_socket(int socket); + diff --git a/backend/windows.c b/backend/windows.c new file mode 100644 index 0000000..6643fee --- /dev/null +++ b/backend/windows.c @@ -0,0 +1,13 @@ +#include "native.h" + +void init_sockets(void) +{ + WSADATA data; + WSAStartup(0x0202, &data); +} + +void close_socket(int socket) +{ + closesocket(socket); +} +