diff options
| author | Oskari Timperi <oskari.timperi@iki.fi> | 2017-02-15 22:52:59 +0200 |
|---|---|---|
| committer | Oskari Timperi <oskari.timperi@iki.fi> | 2017-02-15 22:52:59 +0200 |
| commit | 7a87b3b724e8580195bd35e3654f8f4a2f45ed3c (patch) | |
| tree | 358beda82489e53f1e34fc38a1fa67ea1b52ec5d | |
| parent | 96fad648096614c13e784f11f0e90a583e567de1 (diff) | |
| download | mqtt-7a87b3b724e8580195bd35e3654f8f4a2f45ed3c.tar.gz mqtt-7a87b3b724e8580195bd35e3654f8f4a2f45ed3c.zip | |
Initialize Winsock on Windows
| -rw-r--r-- | src/socket.c | 25 |
1 files changed, 24 insertions, 1 deletions
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 <string.h> #include <stdio.h> @@ -16,6 +17,20 @@ #include <arpa/inet.h> #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 |
