UPDATE:我更新了代码和问题描述以反映我的更改。
我现在知道我正在尝试对非套接字进行套接字操作。或者我的 fd_set 无效,因为:
select
返回 -1 和WSAGetLastError()
返回 10038。
但我似乎无法弄清楚它是什么。平台是Windows。我还没有发布过WSAStartup
part.
int loop = 0;
FILE *output
int main()
{
fd_set fd;
output = _popen("tail -f test.txt","r");
while(forceExit == 0)
{
FD_ZERO(&fd);
FD_SET(_fileno(output),&fd);
int returncode = select(_fileno(output)+1,&fd,NULL,NULL,NULL);
if(returncode == 0)
{
printf("timed out");
}
else if (returncode < 0)
{
printf("returncode: %d\n",returncode);
printf("Last Error: %d\n",WSAGetLastError());
}
else
{
if(FD_ISSET(_fileno(output),&fd))
{
if(fgets(buff, sizeof(buff), output) != NULL )
{
printf("Output: %s\n", buff);
}
}
else
{
printf(".");
}
}
Sleep(500);
}
return 0;
}
现在的新结果当然是打印出返回码和最后一个错误。
您有一些数据可供读取,但您实际上并未读取任何内容。当您下次轮询描述符时,数据仍然存在。在继续轮询之前先排空管道。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)