如何接受超时插座 [英] How to accept socket with timeout

查看:211
本文介绍了如何接受超时插座的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

时出现超时跨平台使用soulution接受客户端接受没有设置插座非阻塞功能?

Is there is a timeout crossplatform soulution to accept client using accept function without setting socket to non-blocking?

我知道,我应该使用函数,但我在做什么错了?

I know that i should use select function to it, but what i'm doing wrong?

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;
}

如何解决呢?
谢谢!

How to fix that? Thanks!

推荐答案

调用的第一个参数应等于在你的FD_SET中人数最多的文件描述符设置加1(见这里)。尝试改变的第一个参数 S + 1 ;你将需要添加一些逻辑,当你有一个以上的插座在您所设定的。

The first parameter of the select call should be equal to the the highest number file descriptor in your fd_set set plus 1 (see here). Try changing the first argument to s+1; you will need to add some logic when your have more than one socket in your set.

这篇关于如何接受超时插座的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆