![]() |
|
VC++, About "accept" function timeout of question! - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Coding (https://sinister.li/Forum-Coding) +--- Forum: C, C++, & Obj-C (https://sinister.li/Forum-C-C-Obj-C) +--- Thread: VC++, About "accept" function timeout of question! (/Thread-VC-About-accept-function-timeout-of-question) |
VC++, About "accept" function timeout of question! - anle - 11-22-2013 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: 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]
VC++, About "accept" function timeout of question! - anle - 11-22-2013 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: 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]
RE: VC++, About "accept" function timeout of question! - CPlus - 11-23-2013 If am not mistaken the value is in microseconds. Code: u_long on = 1;
timeval tv = {0, 900000}; //timeout value in microseconds
fd_set fds;
FD_ZERO(&fds);RE: VC++, About "accept" function timeout of question! - anle - 11-24-2013 I don't think this is reason, because my others timeout coding is no abnormal. RE: VC++, About "accept" function timeout of question! - anle - 11-24-2013 I don't think this is reason, because my others timeout coding is no abnormal. |