gpt4 book ai didi

c++ - select() 不使用为 timeval 设置的内容

转载 作者:太空宇宙 更新时间:2023-11-04 16:16:04 25 4
gpt4 key购买 nike

我在下面有一个片段 - 我已经以多种方式调整了我的 tv_usec,但是我一直坐在 select() 循环中几乎整整 10 秒,无论tv_usec 的设置。

    char buffer[512];
fd_set readfds;
struct timeval tv;
tv.tv_usec = 50;

int rv = 1;

// clear the set ahead of time
FD_ZERO(&readfds);

// add our descriptors to the set
FD_SET(mySocket, &readfds);

// the n param for select()
int n = mySocket + 1;

while(rv != 0)
{
rv = select(n, &readfds, NULL, NULL, &tv);

if (rv == -1)
perror("select"); // error occurred in select()

bzero(buffer,512);
int n = recvfrom(mySocket,buffer,512,0,(struct sockaddr *)&server, &sockLen);

// do stuff...
}

最佳答案

tv.tv_sec 需要初始化为某物。设置 tv.tv_sec = 0 解决了这个问题。

关于c++ - select() 不使用为 timeval 设置的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22470658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com