如果反应时间过长,就要取消这次connect,返回connect失败,
如何实现这样的控制呢?
5 个解决方案
#1
用select 和 timeout 来控制
struct timeval timeout;//
//设置socket为非锁定模式,ul为0值的话,那么soocket将被设置为锁定模式
iErr=ioctlsocket(s,FIONBIO,(unsigned long*)&ul);
if(iErr==SOCKET_ERROR )
{
ShowError("ioctlsocket");
ExitProcess(1);
}
//printf("\n%X ioctl ok.strat conn",i);
//connect to target
connect(s,(struct sockaddr *)&server,sizeof(server));
//printf("\n%X conn return,start select w",i);
//设置select参数
FD_ZERO(&w);
FD_SET(s, &w);
//等待connect成功&socket可写
iErr=select(0, 0, &w, 0, &timeout);
//printf("\n%X select w return %d",i,iErr);
//等待返回后,socket仍不可写则退出
if((iErr==SOCKET_ERROR) || (iErr==0))
{
//printf("\n%X select return w err,exit",i);
__leave;
}
//socket可写则继续
else
{
//send buff to target
//printf("\n%X send",i);
iErr=send(s,SendBuff,SendBuffLen,0);
//printf("\n%X send return",i);
if(iErr==SOCKET_ERROR)
__leave;
}
//等待socket可读
FD_ZERO(&r);
FD_SET(s, &r);
//printf("\n%X start select r",i);
iErr=select(0, &r, 0, 0, &timeout);
//printf("\n%X select r return %d",i,iErr);
if((iErr==SOCKET_ERROR) || (iErr==0))
{
//printf("\n%X select r err,exit",i);
__leave;
}
struct timeval timeout;//
//设置socket为非锁定模式,ul为0值的话,那么soocket将被设置为锁定模式
iErr=ioctlsocket(s,FIONBIO,(unsigned long*)&ul);
if(iErr==SOCKET_ERROR )
{
ShowError("ioctlsocket");
ExitProcess(1);
}
//printf("\n%X ioctl ok.strat conn",i);
//connect to target
connect(s,(struct sockaddr *)&server,sizeof(server));
//printf("\n%X conn return,start select w",i);
//设置select参数
FD_ZERO(&w);
FD_SET(s, &w);
//等待connect成功&socket可写
iErr=select(0, 0, &w, 0, &timeout);
//printf("\n%X select w return %d",i,iErr);
//等待返回后,socket仍不可写则退出
if((iErr==SOCKET_ERROR) || (iErr==0))
{
//printf("\n%X select return w err,exit",i);
__leave;
}
//socket可写则继续
else
{
//send buff to target
//printf("\n%X send",i);
iErr=send(s,SendBuff,SendBuffLen,0);
//printf("\n%X send return",i);
if(iErr==SOCKET_ERROR)
__leave;
}
//等待socket可读
FD_ZERO(&r);
FD_SET(s, &r);
//printf("\n%X start select r",i);
iErr=select(0, &r, 0, 0, &timeout);
//printf("\n%X select r return %d",i,iErr);
if((iErr==SOCKET_ERROR) || (iErr==0))
{
//printf("\n%X select r err,exit",i);
__leave;
}
#2
connect本来就有超时一般是75m.如果你还觉得长你就要把socket 设置为非阻塞模式,然后connect 再select
#3
用select就可以实现超时机制了
#4
学习
#5
就按一楼的做吧。设置SOCKET的超时选项。
#1
用select 和 timeout 来控制
struct timeval timeout;//
//设置socket为非锁定模式,ul为0值的话,那么soocket将被设置为锁定模式
iErr=ioctlsocket(s,FIONBIO,(unsigned long*)&ul);
if(iErr==SOCKET_ERROR )
{
ShowError("ioctlsocket");
ExitProcess(1);
}
//printf("\n%X ioctl ok.strat conn",i);
//connect to target
connect(s,(struct sockaddr *)&server,sizeof(server));
//printf("\n%X conn return,start select w",i);
//设置select参数
FD_ZERO(&w);
FD_SET(s, &w);
//等待connect成功&socket可写
iErr=select(0, 0, &w, 0, &timeout);
//printf("\n%X select w return %d",i,iErr);
//等待返回后,socket仍不可写则退出
if((iErr==SOCKET_ERROR) || (iErr==0))
{
//printf("\n%X select return w err,exit",i);
__leave;
}
//socket可写则继续
else
{
//send buff to target
//printf("\n%X send",i);
iErr=send(s,SendBuff,SendBuffLen,0);
//printf("\n%X send return",i);
if(iErr==SOCKET_ERROR)
__leave;
}
//等待socket可读
FD_ZERO(&r);
FD_SET(s, &r);
//printf("\n%X start select r",i);
iErr=select(0, &r, 0, 0, &timeout);
//printf("\n%X select r return %d",i,iErr);
if((iErr==SOCKET_ERROR) || (iErr==0))
{
//printf("\n%X select r err,exit",i);
__leave;
}
struct timeval timeout;//
//设置socket为非锁定模式,ul为0值的话,那么soocket将被设置为锁定模式
iErr=ioctlsocket(s,FIONBIO,(unsigned long*)&ul);
if(iErr==SOCKET_ERROR )
{
ShowError("ioctlsocket");
ExitProcess(1);
}
//printf("\n%X ioctl ok.strat conn",i);
//connect to target
connect(s,(struct sockaddr *)&server,sizeof(server));
//printf("\n%X conn return,start select w",i);
//设置select参数
FD_ZERO(&w);
FD_SET(s, &w);
//等待connect成功&socket可写
iErr=select(0, 0, &w, 0, &timeout);
//printf("\n%X select w return %d",i,iErr);
//等待返回后,socket仍不可写则退出
if((iErr==SOCKET_ERROR) || (iErr==0))
{
//printf("\n%X select return w err,exit",i);
__leave;
}
//socket可写则继续
else
{
//send buff to target
//printf("\n%X send",i);
iErr=send(s,SendBuff,SendBuffLen,0);
//printf("\n%X send return",i);
if(iErr==SOCKET_ERROR)
__leave;
}
//等待socket可读
FD_ZERO(&r);
FD_SET(s, &r);
//printf("\n%X start select r",i);
iErr=select(0, &r, 0, 0, &timeout);
//printf("\n%X select r return %d",i,iErr);
if((iErr==SOCKET_ERROR) || (iErr==0))
{
//printf("\n%X select r err,exit",i);
__leave;
}
#2
connect本来就有超时一般是75m.如果你还觉得长你就要把socket 设置为非阻塞模式,然后connect 再select
#3
用select就可以实现超时机制了
#4
学习
#5
就按一楼的做吧。设置SOCKET的超时选项。