Basic server, needs to be tested, now need to write client code

This commit is contained in:
Krzosa Karol
2026-01-27 09:03:34 +01:00
parent b4abfb076f
commit 7dcc185c32

View File

@@ -1,6 +1,6 @@
/* /*
- [ ] Setup so that both client and server are in one program? - [ ] Create threads and put client code, server code on separate
- [ ] I would like to make it nicely visible in debugger, I don't want to switch etc. - [ ] Create client code
*/ */
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN
@@ -173,10 +173,9 @@ void serialize_addrinfo(struct addrinfo* in) {
} }
} }
#define PORT "8000" #define PORT "8000"
int main() { void run_server() {
WSADATA wsaData; WSADATA wsaData;
int err_wsa_startup = WSAStartup(MAKEWORD(2, 2), &wsaData); int err_wsa_startup = WSAStartup(MAKEWORD(2, 2), &wsaData);
@@ -236,6 +235,32 @@ int main() {
debugf("waiting for connections..."); debugf("waiting for connections...");
sockaddr_storage their_addr = {};
while (1) {
int their_addrlen = sizeof(their_addr);
SOCKET conn_sock = accept(socket_fd, (sockaddr *)&their_addr, &their_addrlen);
if (conn_sock == -1) {
debugf("failed to accept connection %s", serialize_error_code(WSAGetLastError()));
continue;
}
defer { closesocket(conn_sock); };
// @todo: this might be a little wrong, also the win32 and unix seem to differ on field names in sockaddr
char s[INET6_ADDRSTRLEN];
sockaddr *n = (sockaddr *)&their_addr;
inet_ntop(their_addr.ss_family, n->sa_data, s, sizeof(s));
debugf("server: got connection from: %s", s);
const char *msg = "Hello world";
int send_err_code = send(conn_sock, msg, (int)strlen(msg), 0);
if (send_err_code == -1) {
debugf("failed to send message %s", serialize_error_code(WSAGetLastError()));
}
}
}
int main() {
run_server();
debugf("exiting..."); debugf("exiting...");
return 0; return 0;
} }