如何将connect到服务器的超时时间,设置短一些?

kwhtggayhsg 2014-04-09 03:14:16
ioctl(sockfd, FIONBIO, &ul);

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


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


select函数设置超时短一些,可用select函数实现吗?能否给个例子?
...全文
226 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
max_min_ 2014-04-09
  • 打赏
  • 举报
回复
引用 5 楼 mujiok2003 的回复:
WSAEWOULDBLOCK
或者EAGAIN,都是属于正常情况!
qq120848369 2014-04-09
  • 打赏
  • 举报
回复
可以看我的code.csdn.net里的simple_io:https://code.csdn.net/qq120848369/simple_io/tree/master
赵4老师 2014-04-09
  • 打赏
  • 举报
回复
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.
mujiok2003 2014-04-09
  • 打赏
  • 举报
回复
引用
这个设置之后,所有recv、accept等函数,都是非阻塞了,还需要配合select函数使用吗?
当发送或接受不能马上完成时, 会立刻返回,错误代码为:

WSAEWOULDBLOCK
The socket is marked as nonblocking and the receive operation would block.
至于是否配合select就看你自己,你可以自己重试发送或接收。 在windows上, 重叠IO可以更灵活,更高效。
AndyStevens 2014-04-09
  • 打赏
  • 举报
回复
void connet_timeout(int signo){}
Signal(SIGALRM, connect_timeout) ;
alarm(..);
if( connect(sockfd, ....) <0)
{
	close(sockfd);
	if(errno == EINTR) ... ;//timeout
}
alarm(0);
不败的拿破仑 2014-04-09
  • 打赏
  • 举报
回复
http://www.2cto.com/kf/201210/160646.html
赵4老师 2014-04-09
  • 打赏
  • 举报
回复
赵4老师 2014-04-09
  • 打赏
  • 举报
回复
参考MSDN98\SAMPLES\VC98\SDK\NETDS\WINSOCK\SIMPLE\IOCTL.C

65,208

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧