当前位置: 技术问答>linux和unix
socket编程中select()的用法,请给一个原代码!
来源: 互联网 发布时间:2015-06-08
本文导语: 我看了很多网上的关于select的用法,都是没有给出一个比较清楚的原代码,很难看明白,自己照着写编译没错,但是根本达不到效果,希望那位大哥大姐给个比较详细的原代码,非常感谢! | ...
我看了很多网上的关于select的用法,都是没有给出一个比较清楚的原代码,很难看明白,自己照着写编译没错,但是根本达不到效果,希望那位大哥大姐给个比较详细的原代码,非常感谢!
|
#man select
EXAMPLE
#include
#include
#include
#include
int
main(void) {
fd_set rfds;
struct timeval tv;
int retval;
/* Watch stdin (fd 0) to see when it has input. */
FD_ZERO(&rfds);
FD_SET(0, &rfds);
/* Wait up to five seconds. */
tv.tv_sec = 5;
tv.tv_usec = 0;
retval = select(1, &rfds, NULL, NULL, &tv);
/* Don't rely on the value of tv now! */
if (retval)
printf("Data is available now.n");
/* FD_ISSET(0, &rfds) will be true. */
else
printf("No data within five seconds.n");
exit(0);
}
……
EXAMPLE
#include
#include
#include
#include
int
main(void) {
fd_set rfds;
struct timeval tv;
int retval;
/* Watch stdin (fd 0) to see when it has input. */
FD_ZERO(&rfds);
FD_SET(0, &rfds);
/* Wait up to five seconds. */
tv.tv_sec = 5;
tv.tv_usec = 0;
retval = select(1, &rfds, NULL, NULL, &tv);
/* Don't rely on the value of tv now! */
if (retval)
printf("Data is available now.n");
/* FD_ISSET(0, &rfds) will be true. */
else
printf("No data within five seconds.n");
exit(0);
}
……