@@ -24,11 +24,12 @@ typedef uint32_t in_addr_t;
2424#define SHUT_RD SD_RECEIVE
2525#define SHUT_WR SD_SEND
2626#define SHUT_RDWR SD_BOTH
27- #ifndef EWOULDBLOCK
27+ #ifdef EWOULDBLOCK
28+ #undef EWOULDBLOCK
29+ #endif
2830#define EWOULDBLOCK WSAEWOULDBLOCK
2931#define poll WSAPoll
3032#endif
31- #endif
3233
3334#include <zlib.h>
3435#include <assert.h>
@@ -54,13 +55,21 @@ static void shutdownSocket(int socket, int flags) {
5455static int setSocketNonblocking (int sock ) {
5556//---------------------------------------------------------------------------------
5657
58+ #ifndef __WIN32__
5759 int flags = fcntl (sock , F_GETFL );
5860
5961 if (flags == -1 ) return -1 ;
6062
6163 int rc = fcntl (sock , F_SETFL , flags | O_NONBLOCK );
6264
6365 if (rc != 0 ) return -1 ;
66+ #else
67+ u_long iMode = 1 ; // non-blocking
68+
69+ int rc = ioctlsocket (sock , FIONBIO , & iMode );
70+
71+ if (rc != NO_ERROR ) return -1 ;
72+ #endif
6473
6574 return 0 ;
6675}
@@ -92,7 +101,11 @@ static int socketError(const char *msg) {
92101//---------------------------------------------------------------------------------
93102int pollSocket (int fd , int events , int timeout ) {
94103//---------------------------------------------------------------------------------
104+ #ifndef __WIN32__
95105 struct pollfd pfd ;
106+ #else
107+ WSAPOLLFD pfd ;
108+ #endif
96109
97110 pfd .fd = fd ;
98111 pfd .events = events ;
@@ -463,6 +476,12 @@ static int addExtraArgs(int len, char *buf, char *extra_args) {
463476
464477#define NRO_ARGS 1000
465478
479+ #ifdef __WIN32__
480+ static void win32_socket_cleanup (void ) {
481+ WSACleanup ();
482+ }
483+ #endif
484+
466485//---------------------------------------------------------------------------------
467486int main (int argc , char * * argv ) {
468487//---------------------------------------------------------------------------------
@@ -595,7 +614,7 @@ int main(int argc, char **argv) {
595614 printf ("WSAStartup failed\n" );
596615 return EXIT_FAILURE ;
597616 }
598- atexit (& WSACleanup );
617+ atexit (& win32_socket_cleanup );
599618#endif
600619
601620 struct in_addr nxaddr ;
0 commit comments