为什么poll()无法在STDIN上接收,是什么原因?

时间:2019-12-25 20:51:23

标签: c sockets fgets polling irc

我正尝试通过套接字将消息(写在终端上)发送到IRC:

   #define BUFFSIZE 1024
   #define MSGSIZE 509
    while((poll_count = poll(pfds, 2, -1)) != -1) {

        if (poll_count == 0)
            break;

        if (pfds[1].revents & POLLIN) {
            numbytes = recv(sockfd, buf, BUFFSIZE, 0);
            buf[numbytes] = '\0'; 
            printf("%s\n", buf);
        }

        if (pfds[0].revents & POLLIN) {
            fgets(msg, 5, stdin);
            rmv_newline(msg); 

            if ((strcmp(msg, "EXIT")) == 0) {
                printf("Exiting...\n");
                break;
            }

            strcat(msg, "\r\n");  // no need to add '\0' since rmv_newline does it for us
            sentbytes = send(sockfd, msg, 6, 0);
        }
}

我从fgets和strcat'\ r \ n'中删除了'\ n'并将其发送。现在,由于sendbytes返回正确的数字,因此发送。

但是,即使send()成功返回,循环也不会在STDIN上启动。我需要在STDIN上输入其他内容,然后它才能工作。

我在这里不知所措。谢谢。

0 个答案:

没有答案