Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在循环中正确使用select()?_C_Linux_Sockets_Networking_Gnu - Fatal编程技术网

如何在循环中正确使用select()?

如何在循环中正确使用select()?,c,linux,sockets,networking,gnu,C,Linux,Sockets,Networking,Gnu,我正在尝试修改我找到的一个示例 例如: #include <stdio.h> #include <sys/time.h> #include <sys/types.h> #include <unistd.h> #define STDIN 0 // file descriptor for standard input int main(void) { struct timeval tv; fd_set readfds; tv.

我正在尝试修改我找到的一个示例

例如:

#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

#define STDIN 0  // file descriptor for standard input

int main(void)
{
   struct timeval tv;
   fd_set readfds;

   tv.tv_sec = 2;
   tv.tv_usec = 500000;

   FD_ZERO(&readfds);
   FD_SET(STDIN, &readfds);

   // don't care about writefds and exceptfds:
   select(STDIN+1, &readfds, NULL, NULL, &tv);

   if (FD_ISSET(STDIN, &readfds))
       printf("A key was pressed!\n");
   else
       printf("Timed out.\n");

   return 0;
}
如果2.5秒后未发送消息,则打印超时,否则按打印键

我试着把它放在一个while循环中:

#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

#define STDIN 0  // file descriptor for standard input

int main(void)
{
   fd_set readfds, temp;
   struct timeval tv;

   FD_ZERO(&readfds);
   FD_ZERO(&temp);
   FD_SET(STDIN, &readfds);

   while(1){
       
   temp = readfds;     
   tv.tv_sec = 2;
   tv.tv_usec = 500000;
   // don't care about writefds and exceptfds:
   if (select(STDIN+1, &temp, NULL, NULL, &tv) == -1)
       printf("err");

   if (FD_ISSET(STDIN, &temp))
   {
       printf("A key was pressed!\n");
   }
   else
       printf("Timed out.\n");
   }
   return 0;
} 

在这段代码中,当我输入一个键时,它会一直打印一个永远按下的键

我在网上读到,我每次都要设置电视变量,但仍然没有帮助

我需要一个临时fd_设置吗
? 我错了什么?

您的代码缺乏正确的缩进可能会让oyu绊倒。请修复它。查找从select获得的返回值以及它对temp内容的含义。另外,-1返回不是错误。您从未从fd读取数据,因此它当然是可读的。
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>

                // Note: STDIN_FILENO, or fileno(stdin)
#define STDIN 0  // file descriptor for standard input

int main(void)
{
   fd_set readfds, temp;
   struct timeval tv;
   int ret,ch;

   FD_ZERO(&readfds);
   FD_ZERO(&temp);
   FD_SET(STDIN, &readfds);

   while(1){

        temp = readfds;
        tv.tv_sec = 2;
        tv.tv_usec = 500000;
        // don't care about writefds and exceptfds:
        ret = select(STDIN+1, &temp, NULL, NULL, &tv) ;
        if (ret == -1) {
                if (errno == EAGAIN) continue; // These are NOT Errors, but natural occuring events
                if (errno == EINTR) continue; // The are reported to avoid your select() to block for too long
                        perror("erreur");
                break;
                }
        else if (ret ==0) {
           printf("Timed out.\n");
                continue;
                }
                // Ok: select has returned > 0; there must be something to read
        if (FD_ISSET(STDIN, &temp)) {
                ch = getc(stdin); // Lookout: stdin is line-buffered
                printf("A key was pressed: %d!\n", ch);
                }
        }
   return 0;
}