From 7a87b3b724e8580195bd35e3654f8f4a2f45ed3c Mon Sep 17 00:00:00 2001 From: Oskari Timperi Date: Wed, 15 Feb 2017 22:52:59 +0200 Subject: Initialize Winsock on Windows --- src/socket.c | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/socket.c b/src/socket.c index dfe4506..7828e27 100644 --- a/src/socket.c +++ b/src/socket.c @@ -1,4 +1,5 @@ #include "socket.h" +#include "log.h" #include #include @@ -16,6 +17,20 @@ #include #endif +#if defined(_WIN32) +static int InitializeWsa() +{ + WSADATA wsa; + int rc; + if ((rc = WSAStartup(MAKEWORD(2, 2), &wsa)) != 0) + { + LOG_ERROR("WSAStartup failed: %d", rc); + return -1; + } + return 0; +} +#endif + int SocketConnect(const char *host, short port) { struct addrinfo hints, *servinfo, *p; @@ -23,6 +38,13 @@ int SocketConnect(const char *host, short port) char portstr[6]; int sock; +#if defined(_WIN32) + if (InitializeWsa() != 0) + { + return -1; + } +#endif + memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; @@ -64,7 +86,8 @@ int SocketConnect(const char *host, short port) int SocketDisconnect(int sock) { #ifdef _WIN32 - return closesocket(sock); + int rc = closesocket(sock); + WSACleanup(); #else return close(sock); #endif -- cgit v1.2.3