相关疑难解决方法(0)

如何接受超时套接字

是否存在超时交叉平台soulution以接受客户端使用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;
}
Run Code Online (Sandbox Code Playgroud)

如何解决?谢谢!

c linux windows

5
推荐指数
1
解决办法
2万
查看次数

标签 统计

c ×1

linux ×1

windows ×1