Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.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
C 套接字的文件描述符不工作_C_Sockets - Fatal编程技术网

C 套接字的文件描述符不工作

C 套接字的文件描述符不工作,c,sockets,C,Sockets,这是我正在使用的代码。每当我向Stdin写入内容时,它都可以工作,但对socket不起作用。它无法进入套接字的循环。我不熟悉套接字编程 void HandleConnection(int socket) { fd_set rfd; struct timeval tv; int retval; printf("%d",socket); MakeNonBlocking(socket); /* Watch stdin (fd 0) to see when it has in

这是我正在使用的代码。每当我向Stdin写入内容时,它都可以工作,但对socket不起作用。它无法进入套接字的循环。我不熟悉套接字编程

void HandleConnection(int socket)
{
  fd_set rfd;
  struct timeval tv;
  int retval;

  printf("%d",socket);
  MakeNonBlocking(socket);

  /* Watch stdin (fd 0) to see when it has input. */
  FD_ZERO(&rfd);

  while(1)
  {
    FD_SET(STDIN, &rfd);
    FD_SET(socket, &rfd);

    /* Wait up to five seconds. */
    tv.tv_sec = 50;
    tv.tv_usec = 0;

    retval = select(2, &rfd,NULL, NULL, &tv);
    if(retval == 0)
    {
        printf("No data within fifty seconds.\n");
        exit(1);
    }
    if(FD_ISSET(socket,&rfd))
    {
        printf("socket wala\n");
        recieve_message(&socket);
        send_message(&socket);
    }
    if(FD_ISSET(STDIN,&rfd))
    {
        printf("stdin wala\n");
        recieve_message(&socket);
        send_message(&socket);
    }
  }
}

您似乎不明白如何使用
select()
nfds
参数。手册页明确说明了这一点:

第一批NFD 在每组中检查描述符;i、 例如,检查描述符集中从0到nfds-1的描述符。(例如: 如果您设置了两个文件描述符“4”和“17”,则NFD不应为“2”,而应为“17+1”或“18”。)

下面是您应该如何重写代码

int maxfd = (socket > STDIN ? socket : STDIN) + 1; /* select() requires the number of FDs to scan, which is max(fds)+1 */

while(1){

  FD_ZERO(&rfd); /* This needs to be done each time through the loop */
  /* Watch stdin (fd 0) to see when it has input. */
  FD_SET(STDIN, &rfd);
  FD_SET(socket, &rfd);

  /* Wait up to five seconds. */
  tv.tv_sec = 50;
  tv.tv_usec = 0;

  retval = select(maxfd, &rfd,NULL, NULL, &tv);
  if(retval == 0)
    {
      printf("No data within fifty seconds.\n");
      exit(1);
    }
  if(retval == -1) /* Check for error */
    {
      perror("Error from select");
      exit(2);
    }
  if(FD_ISSET(socket,&rfd))
    {
      printf("socket wala\n");
      recieve_message(&socket);
      send_message(&socket);
    }
  if(FD_ISSET(STDIN,&rfd))
    {
      printf("stdin wala\n");
      recieve_message(&socket);
      send_message(&socket);
    }

 }
  • 在循环中,FDZERO必须在FDSET之前
  • select(2,…)应为select(最高文件描述符+1,…)
  • 当select返回时,如果出现错误,应检查负值
  • 您应该考虑使用pselect而不是select。
  • 在重新初始化电视之前,请先将其清除

  • 您需要将
    FD\u ZERO(&rfd)
    放入循环中。您需要正确地将第一个参数设置为
    select()
    ,2可能不是正确的值,它应该是
    max(STDIN,socket)+1
    @EJP我在更改旁边添加了注释谢谢!它起作用了……我想“2”是我给出的文件描述符的数量。:)我在我的回答中添加了一段参考资料,我认为应该为您澄清这一点。