c - Can the select function be used in windows for input with timeout? -


the windows documentation mentions select function using sockets, gnu c documentation mentions can used input file descriptor.

can select function used in windows input stdin?

here simple program written using c select function can compile gcc

#include <cstdio> #include <ctime> #include <sys/types.h>  int input_timeout (int filedes, unsigned int seconds) {     fd_set set;     struct timeval timeout;      /* initialize file descriptor set. */     fd_zero (&set);     fd_set (filedes, &set);      /* initialize timeout data structure. */     timeout.tv_sec = seconds;     timeout.tv_usec = 0;      /* select returns 0 if timeout, 1 if input available, -1 if error. */     return select (filedes + 1, &set, null, null, &timeout); }  int main (void) {     fprintf (stderr, "select returned %d.\n", input_timeout (fileno(stdin), 5));     return 0; } 

here similar program written windows compiles cl

#include <winsock2.h>  #pragma comment (lib, "ws2_32.lib") #pragma comment (lib, "mswsock.lib")  #include <cstdio> #include <ctime> #include <sys/types.h>   int input_timeout (int filedes, unsigned int seconds) {     struct fd_set set;     struct timeval timeout;      /* initialize file descriptor set. */     fd_zero (&set);     fd_set (filedes, &set);      /* initialize timeout data structure. */     timeout.tv_sec = seconds;     timeout.tv_usec = 0;      /* select returns 0 if timeout, 1 if input available, -1 if error. */     return select (filedes + 1, &set, null, null, &timeout); }  int main (void) {     fprintf (stderr, "select returned %d.\n", input_timeout (_fileno(stdin), 5));     return 0; } 

the first works expected, returning 1 if theres input, 0 if times out

the second returns -1


Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -