Tag: 选择

如何使用stdout选择?

我有以下代码 fd_set set; struct timeval timeout; printf(“first printf\n”); // displayed FD_ZERO(&set); timeout.tv_sec = 1; FD_SET(fileno(stdout), &set); if (select(FD_SETSIZE, NULL, &set, NULL, &timeout)!=1) { stdout_closed = true; return; } printf(“second printf\n”); // Not displayed 我试图在printf(“second printf\n”);之前检查写入stdout的能力printf(“second printf\n”); 。 但是使用此代码,select返回一个值!= 1然后printf保持不可缓存。 看起来像选择返回“ 不可能 ”写入标准输出。 你能解释一下这种行为吗?

如何循环select()无限制地轮询数据

#include #include #include #include int main () { char name[20]; fd_set input_set; struct timeval timeout; int ready_for_reading = 0; int read_bytes = 0; /* Empty the FD Set */ FD_ZERO(&input_set ); /* Listen to the input descriptor */ FD_SET(0, &input_set); /* Waiting for some seconds */ timeout.tv_sec = 10; // 10 seconds timeout.tv_usec = 0; […]

这个_popen / select示例有什么问题?

更新:我更新了代码和问题描述以反映我的更改。 我现在知道我正在尝试在nonsocket上进行Socket操作。 或者我的fd_set无效,因为: select返回-1, WSAGetLastError()返回10038。 但我似乎无法弄清楚它是什么。 平台是Windows。 我还没有发布WSAStartup部分。 int loop = 0; FILE *output int main() { fd_set fd; output = _popen(“tail -f test.txt”,”r”); while(forceExit == 0) { FD_ZERO(&fd); FD_SET(_fileno(output),&fd); int returncode = select(_fileno(output)+1,&fd,NULL,NULL,NULL); if(returncode == 0) { printf(“timed out”); } else if (returncode < 0) { printf("returncode: %d\n",returncode); printf("Last Error: %d\n",WSAGetLastError()); } else { […]

使用select来读取socket和stdin

我正在写一个基于ncurses的聊天程序。 起初,我只编写网络内容(没有ncurses),一切正常,但添加图形后,我无法使客户端应用程序正常工作。 主要问题是同时从stdin和socket读取。 在ncurses-less版本中,我使用了pthread,它就像魅力一样。 唉,似乎pthread和ncurses没有很好地结合在一起,所以我不得不寻找另一种解决方案。 我认为select()会这样做,但它仍然只从stdin读取并完全忽略套接字。 这是整个代码: 代码 有趣的是: char message[1024]; fd_set master; fd_set read_fds; FD_ZERO(&master); FD_ZERO(&read_fds); FD_SET(0,&master); FD_SET(s,&master); // s is a socket descriptor while(true){ read_fds = master; if (select(2,&read_fds,NULL,NULL,NULL) == -1){ perror(“select:”); exit(1); } // if there are any data ready to read from the socket if (FD_ISSET(s, &read_fds)){ n = read(s,buf,max); buf[n]=0; […]

c中有刺激性的select()行为

while (xxx) { timeout.tv_sec=TIMEOUT; timeout.tv_usec=0; FD_ZERO(&set); FD_SET(sd,&set); switch (select(FD_SETSIZE,&set,NULL,NULL,&timeout)) xxxxx } 然而,工作正常 FD_ZERO(&set); FD_SET(sd,&set); while (xxx) { timeout.tv_sec=TIMEOUT; timeout.tv_usec=0; switch (select(FD_SETSIZE,&set,NULL,NULL,&timeout)) xxxxx } 没有。 它第一次工作,但下次它运行while循环时,即使sd套接字接收数据,它也会获得超时。 在我看来,每次都必须清空和填充设置是浪费资源。 任何人都有一个很好的解释为什么这是,甚至更好,也许是一个如何避免它的建议?

检查套接字文件描述符是否可用?

如果我有一个文件描述符(socket fd),如何检查这个fd是否可用于读/写? 在我的情况下,客户端已连接到服务器,我们知道fd。 但是,服务器会断开套接字,是否有任何检查线索?

如何区分Escape和Escape Sequence

我的最终目标是区分我在键盘上按下Esc (ASCII 27 ),然后按下我键盘上的→键(转换为27 91 67的序列)。 我正在使用termios将我的终端设置为非Canonical模式。 我想我明白有两种选择: 等待一段任意的时间来看看是否有东西进入(似乎是hacky) 检查STDIN以查看它是否为空 我正在尝试做后者。 为此,我试图使用select来查看stdin是否为空。 问题 select似乎总是返回0(超时到期)。 这似乎很奇怪,原因有两个: 我想如果我在击中Esc之后没有输入任何内容,那么它将返回-1,因为它没有看到stdin中有任何东西要读 我想如果我输入→ ,那么我会得到一个1因为它看到27之后就有91和67来读 这些事情都没有发生,所以我害怕我只是不理解select或标准输入/输出就像我想的那样。 问题 为什么在我的例子中没有select返回0以外的任何东西? 是否可以检查stdin是否为空? 其他库如何处理这个? 最小,完整和可validation的示例 我在MacOS High Sierra和Ubuntu 16上运行它,结果相同。 资源: #include #include #include #include #include #include #include #include int main() { // put terminal into non-canonical mode struct termios old; struct termios new; int fd = […]

select()如何等待常规文件描述符(非套接字)?

这是来自“man select”的代码示例加上几行来读取正在写入的实际文件。 我怀疑当写入./myfile.txt时, select会返回它现在可以从该fd读取的内容。 但是,只要txt文件存在,select就会在while循环中不断返回。 我希望它只在新数据写入文件末尾时返回。 我认为这应该是如何运作的。 #include #include #include #include #include #include int main(void) { fd_set rfds; struct timeval tv; int retval; int fd_file = open(“/home/myfile.txt”, O_RDONLY); /* Watch stdin (fd 0) to see when it has input. */ FD_ZERO(&rfds); FD_SET(0, &rfds); FD_SET(fd_file, &rfds); /* Wait up to five seconds. */ tv.tv_sec = 5; […]

在非阻塞套接字连接中,select()始终返回1

我有这个代码段,旨在使用套接字连接连接到服务器。 但是,如果它无法在一定时间内连接到服务器,我希望它停止尝试。 我尝试使用这个非阻塞套接字和select命令执行此操作,但select总是返回1,表示当我提供的地址不存在时服务器存在。 有任何想法吗? SOCKET tcp_client( char *hname, char *sname ) { fd_set fdset; struct sockaddr_in peer; SOCKET s; FD_ZERO(&fdset); // FD_SET(STDIN, &fdset); FD_SET(s, &fdset); errno=1; struct timeval tv; tv.tv_sec = 15; set_address( hname, sname, &peer, “tcp” ); s = socket( AF_INET, SOCK_STREAM, 0 ); int n = 1; fcntl(s, F_SETFL, O_NONBLOCK); if ( !isvalidsock( […]

select()用于的nfds是什么?

我想知道nfds做了什么,在阅读了不同的手册之后,我最终得到的唯一答案是它是编号最高的文件描述符加一 。 它究竟用于什么?