summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDevyn Challman <devyn@challman.org>2026-03-27 14:40:49 -0700
committerDevyn Challman <devyn@challman.org>2026-03-27 14:40:49 -0700
commit2011d396bd9e36737130612dfe37f323b7646011 (patch)
tree3a0d927c6c5518676eea6638499a73fbf06913ee
parenta0f8dc499b60bb326a8765bf435c4a4344df0b20 (diff)
add basic udp server and client
-rw-r--r--Makefile21
-rw-r--r--client/main.c22
-rw-r--r--client/net/net.c26
-rw-r--r--client/net/net.h1
-rw-r--r--server/main.c42
5 files changed, 95 insertions, 17 deletions
diff --git a/Makefile b/Makefile
index a14dbf8..65ffe9d 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,6 @@
CC = gcc-15
-CFLAGS = -Wall -Wextra
+CFLAGS = -MMD -MP -Wall -Wextra -pthread
+LDFLAGS = -pthread
SERVER_BUILD = build/server
SERVER_TARGET = bin/server
@@ -11,22 +12,26 @@ CLIENT_TARGET = bin/client
CLIENT_SRC = $(shell find client -name "*.c")
CLIENT_OBJ = $(CLIENT_SRC:client/%.c=$(CLIENT_BUILD)/%.o)
CLIENT_CFLAGS = $(CFLAGS) $(shell pkg-config --cflags raylib)
-CLIENT_LDFLAGS = $(shell pkg-config --libs raylib)
+CLIENT_LDFLAGS = $(LDFLAGS) $(shell pkg-config --libs raylib)
-all: $(SERVER_TARGET) $(CLIENT_TARGET)
+-include $(SERVER_OBJ:.o=.d)
+-include $(CLIENT_OBJ:.o=.d)
.PHONY: all clean server client
+all: $(SERVER_TARGET) $(CLIENT_TARGET)
+
server: $(SERVER_TARGET)
client: $(CLIENT_TARGET)
-$(SERVER_TARGET): $(SERVER_OBJ)
+bin:
mkdir -p bin
- $(CC) $(CFLAGS) $(SERVER_OBJ) -o $(SERVER_TARGET)
-$(CLIENT_TARGET): $(CLIENT_OBJ)
- mkdir -p bin
- $(CC) $(CLIENT_CFLAGS) $(CLIENT_OBJ) $(CLIENT_LDFLAGS) -o $(CLIENT_TARGET)
+$(SERVER_TARGET): $(SERVER_OBJ) | bin
+ $(CC) $(LDFLAGS) $(SERVER_OBJ) -o $(SERVER_TARGET)
+
+$(CLIENT_TARGET): $(CLIENT_OBJ) | bin
+ $(CC) $(CLIENT_LDFLAGS) $(CLIENT_OBJ) -o $(CLIENT_TARGET)
$(SERVER_BUILD)/%.o: server/%.c
mkdir -p $(dir $@)
diff --git a/client/main.c b/client/main.c
index a38f470..c0ed27e 100644
--- a/client/main.c
+++ b/client/main.c
@@ -1,5 +1,6 @@
#include "raylib.h"
#include <stdio.h>
+#include "net/net.h"
int main() {
const int screenWidth = 800;
@@ -29,28 +30,33 @@ int main() {
while (!WindowShouldClose()) {
bool connecting = false;
- if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) {
+ if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) {
struct Vector2 mousePos = GetMousePosition();
if (mousePos.x >= buttonPosX && mousePos.x <= buttonPosX + buttonW &&
mousePos.y >= buttonPosY && mousePos.y <= buttonPosY + buttonH) {
- connecting = true;
+ connecting = true;
}
}
BeginDrawing();
ClearBackground(RAYWHITE);
- DrawText(message, messagePosX, messagePosY, fontSize, LIGHTGRAY);
- DrawRectangle(buttonPosX, buttonPosY, buttonW, buttonH, GREEN);
+ DrawText(message, messagePosX, messagePosY, fontSize, LIGHTGRAY);
- if(connecting)
- DrawText(connect, connectPosX, connectPosY, fontSize, RED);
- else
- DrawText(connect, connectPosX, connectPosY, fontSize, BLUE);
+ if(connecting) {
+ DrawRectangle(buttonPosX, buttonPosY, buttonW, buttonH, RED);
+ } else {
+ DrawRectangle(buttonPosX, buttonPosY, buttonW, buttonH, GREEN);
+ }
+
+ DrawText(connect, connectPosX, connectPosY, fontSize, BLUE);
DrawFPS(0,0);
EndDrawing();
+
+ if (connecting)
+ ServerConnect();
}
return 0;
diff --git a/client/net/net.c b/client/net/net.c
new file mode 100644
index 0000000..be61104
--- /dev/null
+++ b/client/net/net.c
@@ -0,0 +1,26 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <err.h>
+#include <sys/socket.h>
+#include <unistd.h>
+#include <netinet/in.h>
+#include <time.h>
+#include "net.h"
+
+void ServerConnect() {
+ int fd = socket(AF_INET, SOCK_DGRAM, 0);
+
+ if (fd == -1)
+ err(EXIT_FAILURE, "socket failed");
+
+ char buff[64];
+
+ struct sockaddr_in server;
+ server.sin_family = AF_INET;
+ server.sin_port = htons(8080);
+ server.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+
+ sendto(fd, buff, sizeof(buff), 0, (struct sockaddr *)&server, sizeof(server));
+
+ close(fd);
+}
diff --git a/client/net/net.h b/client/net/net.h
new file mode 100644
index 0000000..7947419
--- /dev/null
+++ b/client/net/net.h
@@ -0,0 +1 @@
+void ServerConnect();
diff --git a/server/main.c b/server/main.c
index 237c8ce..789d392 100644
--- a/server/main.c
+++ b/server/main.c
@@ -1 +1,41 @@
-int main() {}
+#include <stdio.h>
+#include <stdlib.h>
+#include <err.h>
+#include <sys/socket.h>
+#include <unistd.h>
+#include <netinet/in.h>
+#include <time.h>
+
+int main() {
+ int fd = socket(AF_INET, SOCK_DGRAM, 0);
+
+ if (fd == -1)
+ err(EXIT_FAILURE, "socket failed");
+
+ struct sockaddr_in address;
+ address.sin_family = AF_INET;
+ address.sin_port = htons(8080);
+ address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+
+ if (bind(fd, (struct sockaddr *)&address, sizeof(address)) == -1) {
+ close(fd);
+ err(EXIT_FAILURE, "bind failed");
+ }
+
+ time_t startTime = time(NULL);
+
+ while (time(NULL) < startTime + 60) {
+ struct sockaddr connection;
+ socklen_t connectionLen = sizeof(connection);
+ char buff[64];
+
+ int size = recvfrom(fd, buff, sizeof(buff), MSG_DONTWAIT,
+ (struct sockaddr *)&connection, &connectionLen);
+
+ if (size != -1)
+ printf("got a message\n");
+ }
+
+ close(fd);
+ return 0;
+}