diff --git a/ports/win32/bip-init.c b/ports/win32/bip-init.c index ac9f8585..01a5451f 100644 --- a/ports/win32/bip-init.c +++ b/ports/win32/bip-init.c @@ -48,6 +48,7 @@ /* Windows sockets */ static SOCKET BIP_Socket = INVALID_SOCKET; static SOCKET BIP_Broadcast_Socket = INVALID_SOCKET; +static bool BIP_Initialized; /* NOTE: we store address and port in network byte order since BACnet/IP uses network byte order for all address byte arrays @@ -258,18 +259,17 @@ static void print_last_error(const char *info) */ static void bip_init_windows(void) { - static bool initialized = false; int Result; WSADATA wd; - if (!initialized) { + if (!BIP_Initialized) { Result = WSAStartup((1 << 8) | 1, &wd); /*Result = WSAStartup(MAKEWORD(2,2), &wd); */ if (Result != 0) { print_last_error("TCP/IP stack initialization failed"); exit(1); } - initialized = true; + BIP_Initialized = true; atexit(bip_cleanup); } } @@ -933,7 +933,10 @@ void bip_cleanup(void) } BIP_Broadcast_Socket = INVALID_SOCKET; - WSACleanup(); + if (BIP_Initialized) { + BIP_Initialized = false; + WSACleanup(); + } return; } diff --git a/ports/win32/bip6.c b/ports/win32/bip6.c index fcd9ce3d..90f113fd 100644 --- a/ports/win32/bip6.c +++ b/ports/win32/bip6.c @@ -375,7 +375,7 @@ int bip6_send_mpdu(BACNET_IP6_ADDRESS *dest, uint8_t *mtu, uint16_t mtu_len) uint16_t addr16[8]; /* assumes that the driver has already been initialized */ - if (BIP6_Socket < 0) { + if (BIP6_Socket == INVALID_SOCKET) { return 0; } /* load destination IP address */ @@ -443,7 +443,7 @@ uint16_t bip6_receive( uint16_t i = 0; /* Make sure the socket is open */ - if (BIP6_Socket < 0) { + if (BIP6_Socket == INVALID_SOCKET) { return 0; } /* we could just use a non-blocking socket, but that consumes all @@ -503,10 +503,11 @@ uint16_t bip6_receive( */ void bip6_cleanup(void) { - if (BIP6_Socket != -1) { - closesocket(BIP6_Socket); + if (BIP6_Socket == INVALID_SOCKET) { + return; } - BIP6_Socket = -1; + closesocket(BIP6_Socket); + BIP6_Socket = INVALID_SOCKET; WSACleanup(); return;