Linux打印程序的全部fd,linux – 如何在socket编程中使用select和FD_SET?

我是socket编程的新手,我无法理解select()和FD_SET()的工作原理.

我修改了Beej教程中的一个例子,试图找出它.我想在for循环中做的是每次迭代我等待4秒.如果有可用的读取,我会打印“按下了一个键”,如果超时,则会打印“超时”.然后我会清除该设置并重复该过程9次.但似乎一旦设置了文件描述符0,即使在调用FD_ZERO()和/或FD_CLR()之后它也永远不会被设置.换句话说,在循环的第一次迭代中按下一个键之后,将为剩余的迭代设置文件描述符,并且不再进行等待.所以一定有我遗失的东西,但我不知道是什么.

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define SERVERPORT 4950

int main(int argc,char *argv[]) {

struct sockaddr_in their_addr; // connector's address information

struct hostent *he;

int numbytes;

int broadcast = 1;

if ((he=gethostbyname(argv[1])) == NULL) { // get the host info

perror("gethostbyname");

exit(1);

}

// this call is what allows broadcast packets to be sent:

if (setsockopt(sockfd,SOL_SOCKET,SO_BROADCAST,&broadcast,sizeof broadcast) == -1) {

perror("setsockopt (SO_BROADCAST)");

exit(1);

}

their_addr.sin_family = AF_INET; // host byte order

their_addr.sin_port = htons(SERVERPORT); // short,network byte order

their_addr.sin_addr = *((struct in_addr *)he->h_addr);

memset(their_addr.sin_zero,'\0',sizeof their_addr.sin_zero);

struct timeval tv;

fd_set broadcastfds;

int i;

for(i=0; i < 10; i++) {

tv.tv_sec = 4;

tv.tv_usec = 500000;

FD_ZERO(&broadcastfds);

FD_CLR(0,&broadcastfds);

FD_SET(0,&broadcastfds);

if(select(0+1,&broadcastfds,NULL,&tv) == -1) perror("select");

if (FD_ISSET(0,&broadcastfds)) printf("A key was pressed!\n");

else printf("Timed out.\n");

fflush(stdout);

}

close(sockfd);

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值