fnet.c

Simple C networking library
git clone git://git.finwo.net/lib/fnet.c
Log | Files | Refs | README

commit 1e28974d6b981b2a4ee4b197e2ee7c481c4ec84d
parent 46d565babed23e51bc83ed69bbc109d1b51a05fb
Author: Yersa Nordman <yersa@finwo.nl>
Date:   Mon,  1 Jan 2024 23:00:24 +0100

Added calls to wsastartup and wsacleanup for windows

Diffstat:
Msrc/fnet.c | 16++++++++++++++++
1 file changed, 16 insertions(+), 0 deletions(-)

diff --git a/src/fnet.c b/src/fnet.c @@ -27,6 +27,7 @@ #if defined(_WIN32) || defined(_WIN64) #define FNET_SOCKET unsigned int #pragma comment(lib,"Ws2_32.lib") +bool w32_initialized = false; #else #define FNET_SOCKET int #endif @@ -134,6 +135,18 @@ struct fnet_internal_t * _fnet_init(const struct fnet_options_t *options) { struct fnet_t * fnet_listen(const char *address, uint16_t port, const struct fnet_options_t *options) { struct fnet_internal_t *conn; +#if defined(_WIN32) || defined(_WIN64) + if (!w32_initialized) { + WSADATA wsaData; + int err = WSAStartup(MAKEWORD(2, 2), &wsaData); + if (err) { + fprintf(stderr, "fnet_listen: WSAStartup\n"); + return NULL; + } + w32_initialized = true; + } +#endif + // Checking arguments are given if (!address) { fprintf(stderr, "fnet_listen: address argument is required\n"); @@ -738,5 +751,8 @@ FNET_RETURNCODE fnet_main() { FNET_RETURNCODE fnet_shutdown() { runners = 0; while(connections) fnet_free((struct fnet_t *)connections); +#if defined(_WIN32) || defined(_WIN64) + WSACleanup(); +#endif return FNET_RETURNCODE_OK; }