VC++, About "accept" function timeout of question! 11-22-2013, 06:21 AM
#1
I want to achieve "accept" function can be timeout. However, after the success of the new connection will be received within the specified timeout off, and I would like to receive new connections can continue to connect.
next is my code:
sorry, my english very poor
[/code]
next is my code:
PHP Code:
......
//Set nonblocking mode connection
unsigned long ul=1;
int ret=ioctlsocket(destsock,FIONBIO,(unsigned long*)&ul);
if(ret==SOCKET_ERROR)
{
closesocket(destsock);
return ;
}
//Set accept timeout
fd_set aptset;
struct timeval timeout;
FD_ZERO(&aptset);
FD_SET(destsock,&aptset);
timeout.tv_sec=30; //after 30 second timeout
timeout.tv_usec=0;
int i=0;
while(1)
{
int ret=select(destsock+1,&aptset,NULL,NULL,&timeout);
if(ret==0) //has timeout
{
i++;
continue;
}
else if(FD_ISSET(destsock,&aptset))
{
if((newsock=accept(destsock,(struct sockaddr *)&client2,&size2))<0)
{
closesocket(destsock);
i=0;
break;
}
i++;
break;
}
else
{
i=0;
closesocket(destsock);
break;
}
}
ul=0;
ret=ioctlsocket(destsock,FIONBIO,(unsigned long*)&ul);
if(i<=0) return ;
while(1)
{
......
result=select(newsock+1,&readfd,&writefd,NULL,×et); //timeset is 60 seconds timeout
if(result<0 && errno!=EINTR)
{ [color=#FF0000]//My problems right here,at 30 seconds then abnormal,why?[/color]
break;
}
else if(...)
{...}
else
{...}
if(FD_ISSET(newsock,&readfd))
{...}
}
......
sorry, my english very poor
[/code]


![[+]](https://sinister.li/images/modern/collapse_collapsed.png)