请问一个用select设置connect超时时间的问题?
在局域网内,如果我用阻塞模式调用connect时就没有问题,如果用select来实现设置connect的超时时间就会出现有时连不上对方的问题(对方程序开着),用select的代码如下,请大家指点,谢谢啦。还有就是TCP连接的time_wait是什么状态啊,谢谢^_^
//set the client socket to be nonblocking mode
unsigned long uArg = 1;
if(SOCKET_ERROR == ioctlsocket(sClient, FIONBIO, &uArg))
{
nErrCode = WSAGetLastError();
strcat(pFunNm, "ioctlsocket");
__leave;
}
if(SOCKET_ERROR==connect(sClient, (sockaddr*)(&SvrAddr), sizeof(SvrAddr)))
{
nErrCode = WSAGetLastError();
if(nErrCode != WSAEWOULDBLOCK )
{
strcat(pFunNm, "connect");
__leave;
}
//else set the error code back
nErrCode = 0;
}
//wait untill finished or timeout
fd_set fdCnn;
timeval seltime;
seltime.tv_sec = nTmOutSecond;
seltime.tv_usec = 0;// time out
FD_ZERO(&fdCnn);
FD_SET(sClient, &fdCnn);
int nRcvNm = 0;
if( (nRcvNm = select(sClient+1, NULL, &fdCnn, NULL, &seltime)) == SOCKET_ERROR)
{
nErrCode = WSAGetLastError();
strcat(pFunNm, "select");
__leave;
}
if(!FD_ISSET(sClient, &fdCnn))
{
nErrCode = OWN_ERR;
strcat(pFunNm, "Timeout, the remote server dosen't respond!");
__leave;
}
//connected ....