网络编程:一步步学习select()

来源:互联网 发布:c语言音乐点播制作 编辑:程序博客网 时间:2024/05/18 18:54

/******************************************************************************************************************
参考:1 http://blog.csdn.net/sunxx1986/article/details/6883909
说明:linux socket的select函数例)。

******************************************************************************************************************/ 


1.只做定时器

#include <stdio.h>#include <unistd.h>#include <sys/select.h>#include <sys/time.h>int main(){int rc = 0;struct timeval tv;tv.tv_sec = 5;tv.tv_usec = 0;while(1){tv.tv_sec = 5;tv.tv_usec = 0;printf("a\n");rc = select(0, NULL, NULL, NULL, &tv);printf("rc = %d\n", rc);if(rc == 0)printf("b\n");elseprintf("error !\n");}return 0;}

这样可以省去考虑的东西很多,就最后一个&tv起作用了,其它一概不考虑,这就是一个比sleep精度稍高的定时器。






原创粉丝点击