怎么将connect到服务器的超时时间,设置短一些

如何将connect到服务器的超时时间,设置短一些?
ioctl(sockfd, FIONBIO, &ul); 

//设置socket为非阻塞模式是什么意思?


这个设置之后,所有recv、accept等函数,都是非阻塞了,还需要配合select函数使用吗?


select函数设置超时短一些,可用select函数实现吗?能否给个例子?
------解决方案--------------------
参考MSDN98\SAMPLES\VC98\SDK\NETDS\WINSOCK\SIMPLE\IOCTL.C

------解决方案--------------------
http://bbs.csdn.net/topics/390374955
------解决方案--------------------
http://www.2cto.com/kf/201210/160646.html
------解决方案--------------------
void connet_timeout(int signo){}
Signal(SIGALRM, connect_timeout) ;
alarm(..);
if( connect(sockfd, ....) <0)
{
close(sockfd);
if(errno == EINTR) ... ;//timeout
}
alarm(0);

------解决方案--------------------
引用
这个设置之后,所有recv、accept等函数,都是非阻塞了,还需要配合select函数使用吗?

当发送或接受不能马上完成时, 会立刻返回,错误代码为:

WSAEWOULDBLOCK
The socket is marked as nonblocking and the receive operation would block.

至于是否配合select就看你自己,你可以自己重试发送或接收。 

在windows上, 重叠IO可以更灵活,更高效。 
------解决方案--------------------
If no error occurs, connect returns zero. Otherwise, it returns SOCKET_ERROR, and a specific error code can be retrieved by calling WSAGetLastError.

On a blocking socket, the return value indicates success or failure of the connection attempt.

With a nonblocking socket, the connection attempt cannot be completed immediately. In this case, connect will return SOCKET_ERROR, and WSAGetLastError will return WSAEWOULDBLOCK. In this case, there are three different steps you can take: 

Use the select function to determine the completion of the connection request by checking to see if the socket is writeable. 
If the application is using WSAAsyncSelect to indicate interest in connection events, then the application will receive an FD_CONNECT notification indicating that the connect operation is complete (successfully or not). 
If the application is using WSAEventSelect to indicate interest in connection events, then the associated event object will be signaled indicating that the connect operation is complete (successfully or not). 
Until the connection attempt completes on a nonblocking socket, all subsequent calls to connect on the same socket will fail with the error code WSAEALREADY, and WSAEISCONN when the connection completes successfully. Due to ambiguities in version 1.1 of the Windows Sockets specification, error codes returned from connect while a connection is already pending may vary among implementations. As a result, it is not recommended that applications use multiple calls to connect to detect connection completion. If they do, they must be prepared to handle WSAEINVAL and WSAEWOULDBLOCK error values the same way that they handle WSAEALREADY, to assure robust execution.

If the error code returned indicates the connection attempt failed (that is, WSAECONNREFUSED, WSAENETUNREACH, WSAETIMEDOUT) the application can call connect again for the same socket.

------解决方案--------------------
可以看我的code.csdn.net里的simple_io:https://code.csdn.net/qq120848369/simple_io/tree/master
------解决方案--------------------
引用:
WSAEWOULDBLOCK
 

 或者EAGAIN,都是属于正常情况!