前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >anetTcpGenericConnect 详解

anetTcpGenericConnect 详解

作者头像
看、未来
发布2020-09-15 10:28:34
4260
发布2020-09-15 10:28:34
举报
文章被收录于专栏:CSDN搜“看,未来”
代码语言:javascript
复制
/*
	参数释义:
		addr:传入服务器地址信息,其中内容将用于创建监听套接字
		source_addr:相当于客户端地址信息
		flags:这我还真没看明白。。。再最后一块儿,注释给出了,如果没猜错,应该是和仅打开监听套接字有关的
*/
static int anetTcpGenericConnect(char *err, const char *addr, int port,const char *source_addr, int flags)
{
    int s = ANET_ERR, rv;
    char portstr[6];  /* strlen("65535") + 1; */
    struct addrinfo hints, *servinfo, *bservinfo, *p, *b;

    snprintf(portstr,sizeof(portstr),"%d",port);
    memset(&hints,0,sizeof(hints));
    hints.ai_family = AF_UNSPEC;	//未指定
    hints.ai_socktype = SOCK_STREAM;//有序、可靠、面向连接的双向字节流

    if ((rv = getaddrinfo(addr,portstr,&hints,&servinfo)) != 0) {	//解析addr信息,存入 servinfo
    //不懂一定要看上面的工具包,写了一晚上呢
        anetSetError(err, "%s", gai_strerror(rv));	//这个不管它,报错函数而已
        return ANET_ERR;
    }
    for (p = servinfo; p != NULL; p = p->ai_next) {		//工具包里有说,给你一个addrinfo,它可能是一串
        /* Try to create the socket and to connect it.
         * If we fail in the socket() call, or on connect(), we retry with
         * the next entry in servinfo. */
        if ((s = socket(p->ai_family,p->ai_socktype,p->ai_protocol)) == -1)	//这里s成为了监听套接字
            continue;	//如果没打开,那就继续循环,无碍
        if (anetSetReuseAddr(err,s) == ANET_ERR) goto error;	//设置地址重用
        if (flags & ANET_CONNECT_NONBLOCK && anetNonBlock(err,s) != ANET_OK)	//设置非阻塞
            goto error;
        if (source_addr) {	//source_addr:传入参数
            int bound = 0;
            /* Using getaddrinfo saves us from self-determining IPv4 vs IPv6 */
            if ((rv = getaddrinfo(source_addr, NULL, &hints, &bservinfo)) != 0)	//内啥,不多说了啊
            {
                anetSetError(err, "%s", gai_strerror(rv));
                goto error;
            }
            for (b = bservinfo; b != NULL; b = b->ai_next) {
                if (bind(s,b->ai_addr,b->ai_addrlen) != -1) {
                    bound = 1;
                    break;
                }
            }
            freeaddrinfo(bservinfo);
            if (!bound) {
                anetSetError(err, "bind: %s", strerror(errno));
                goto error;
            }
        }	//for循环到这里结束
        if (connect(s,p->ai_addr,p->ai_addrlen) == -1) {
            /* If the socket is non-blocking, it is ok for connect() to
             * return an EINPROGRESS error here. */
            if (errno == EINPROGRESS && flags & ANET_CONNECT_NONBLOCK)
                goto end;
            close(s);
            s = ANET_ERR;
            continue;
        }

        /* If we ended an iteration of the for loop without errors, we
         * have a connected socket. Let's return to the caller. */
        goto end;
    }
    if (p == NULL)
        anetSetError(err, "creating socket: %s", strerror(errno));

error:
    if (s != ANET_ERR) {
        close(s);
        s = ANET_ERR;
    }

end:
    freeaddrinfo(servinfo);

    /* Handle best effort binding: if a binding address was used, but it is
     * not possible to create a socket, try again without a binding address. */
    if (s == ANET_ERR && source_addr && (flags & ANET_CONNECT_BE_BINDING)) {	
    //#define ANET_CONNECT_BE_BINDING 2 /* Best effort binding. */
        return anetTcpGenericConnect(err,addr,port,NULL,flags);
    } else {
        return s;
    }
}

如果还有什么疑惑,可以在评论区留言一起讨论哦。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/09/11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档