是否存在超时跨平台解决方案来接受客户端使用accept
功能而不将套接字设置为非阻塞?
我知道我应该使用select
功能,但我做错了什么?
SOCKET NativesAcceptClient(SOCKET s, int timeout)
{
int iResult;
struct timeval tv;
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(s, &rfds);
tv.tv_sec = (long)timeout;
tv.tv_usec = 0;
iResult = select(s, &rfds, (fd_set *) 0, (fd_set *) 0, &tv);
if(iResult > 0)
{
return accept(s, NULL, NULL);
}
else
{
//always here, even if i connect from another application
}
return 0;
}
如何解决?谢谢!