linux socket connect [error:EINPROGRESS]一般会在什么情况下产生?
ret = connect(fd, (struct sockaddr*)&server, sizeof(server)));
调用connect然后返回-1,查了下文档是这么说的
EINPROGRESS
----------
The socket is nonblocking and the connection cannot be completed immediately. It is possible to select(2) or poll(2) for completion by selecting the socket for writing. After select(2) indicates writability, use getsockopt(2) to read the SO_ERROR option at level SOL_SOCKET to determine whether connect() completed successfully (SO_ERROR is zero) or unsuccessfully (SO_ERROR is one of the usual error codes listed here, explaining the reason for the failure).
现在疑惑的是在哪些情况下connetc有可能会返回EINPROGRESS,以及对于这种情况我做如下的处理是否得当?
int err = -1;
socklen_t len = sizeof(int);
if (getsockopt(fd, SOL_SOCKET, SO_ERROR ,&err, &len) < 0 ){
close(fd);
logError("errno:%d %s\n", errno, strerror(errno));
}
if (err){
errno = err;
close(fd);
logError("%d :Get socketopt err: %d", __LINE__, err);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
非阻塞的socket,connect调用后立即返回,连接过程还在执行