更新:我更新了代码和问题描述以反映我的更改。这个_popen/select示例有什么问题?
我现在知道我正在尝试Socket套接字操作的非套接字。或者说我的fd_set是无效的,因为:
select
返回-1, WSAGetLastError()
收益10038
,但我似乎无法弄清楚它是什么。平台是Windows。我没有发布WSAStartup
部分。
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;
}
新的结果现在当然是打印出来的返回码和最后一个错误。
您对有关第一个参数选择需要正确设置的评论对于Berkeley套接字是正确的。在Windows中不是这样。该参数未使用(虽然它应该仍然是正确设置恕我直言!)。 Poseter没有指定平台,所以我想我会指出平台的不同。平台确实是Windows的 – Mark 2008-09-30 12:49:28
。对不起,没有发布 – SinisterDex 2008-09-30 13:00:48