打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
socket:operation now in process

socket:operation now in process


正文共: 1643字 0图

预计阅读时间: 5分钟

错误码释义

man connect:

EINPROGRESS
 

The  socket  is  non-blocking  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).

翻译

套接字是非阻塞的,链接并不能立即建立,可用select(2)或者poll(2)等待完成。等待select(2)标识套接字可写的时候,用getsockopt(2)在socket的SOL_SOCKET层读取SO_ERROR错误码来判断链接是否成功,SO_ERROR为0标识着链接成功。

下面是非阻塞连接的实现

#include     "unp.h"

int connect_nonb(int sockfd, const SA *saptr, socklen_t salen, int nsec)
{
    int     flags, n, error;
    socklen_t len;
    fd_set rset, wset;
    struct timeval tval;

    flags = Fcntl(sockfd, F_GETFL, 0);
    Fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);

    error = 0;
    if ( (n = connect(sockfd, saptr, salen)) < 0)
        if (errno != EINPROGRESS)
            return (-1);

    /* Do whatever we want while the connect is taking place. */

    if (n == 0)
        goto done;               /* connect completed immediately */

    FD_ZERO(&rset);
    FD_SET(sockfd, &rset);
    wset = rset;
    tval.tv_sec = nsec;
    tval.tv_usec = 0;

    if ( (n = Select(sockfd + 1, &rset, &wset, NULL,
                    nsec ? &tval : NULL)) == 0) {
        close(sockfd);          /* timeout */
        errno = ETIMEDOUT;
        return (-1);
    }

    if (FD_ISSET(sockfd, &rset) || FD_ISSET(sockfd, &wset)) {
        len = sizeof(error);
        if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
            return (-1);     /* Solaris pending error */
    } else
        err_quit("select error: sockfd not set");

  done:
    Fcntl(sockfd, F_SETFL, flags);  /* restore file status flags */

    if (error) {
        close(sockfd);           /* just in case */
        errno = error;
        return (-1);
    }
    return (0);
}

之所以会出现operation now in process,是因为connect_non执行到select处,等待连接建立时,connect又被执行到了,所以会有错误operation now in process出现。

本错误解读基于作者的能力,有不对的地方敬请指正。

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
非阻塞connect的实现
socket之send与发送缓冲区大小的关系
linux应用编程之网络(4)——非阻塞connect的使用及其它
对于使用select()的操作有没有必要把IO设置为非阻塞?
Linux下getsockopt/setsockopt 函数说明 - xioahw的专栏 ...
套接字选项(四)_深度探索Linux内核
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服