Linux----网络编程(IO复用服务器多客户端——将键盘输入的内容通过read打印出来,若键盘无输入则输出time out)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_41026740/article/details/83353215

服务器ser_key.c

#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <sys/select.h>
#include <sys/time.h>

#define STDIN 0

int main()
{
	int fd = STDIN;
	fd_set fdset;//创建集合
	while(1)
	{
		FD_ZERO(&fdset);//将集合清空
		DS_SET(fd, &fdset);

		struct timeval tv = {5, 0};//5秒0微秒
		int n = select(STDIN+1, &fdset, NULL, NULL, &tv);
		if(n < 0)
		{
			continue;
		}
		else if(n == 0)
		{
			printf("time out!\n");
			continue;
		}
		else
		{
			char buff[128] = {0};
			read(fd, buff, 127);//读取集合里的数据
			printf("read = %s\n", buff);
		}
	}
}

结果:

猜你喜欢

转载自blog.csdn.net/qq_41026740/article/details/83353215
今日推荐