linux 下建立异步链接

int32_t SockEndPoint::connect_server()
{
	struct sockaddr_in client_addr;
    int sock_ret = 0;
    bzero(&client_addr, sizeof(client_addr) );
    client_addr.sin_family = AF_INET;
    client_addr.sin_addr.s_addr = htons(INADDR_ANY);
    client_addr.sin_port = htons(0);    

    client_socket_ = socket( AF_INET, SOCK_STREAM, 0);
    if ( client_socket_ < 0)
    {
        LOGV(LL_ERROR, "Create Socket Failed!\n" );
        return -1;
    }
    /*
    if ( bind( client_socket_ , (struct sockaddr*)&client_addr, sizeof(client_addr)))
    {
        LOGV(LL_ERROR, "Client Bind Port Failed!\n" ); 
        return -2;
    }*/
    struct sockaddr_in server_addr;
    bzero(&server_addr, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    if (inet_aton(addr_.c_str() , &server_addr.sin_addr) == 0)
    {
        LOGV(LL_ERROR, "Server IP Address Error!\n" );
        return -3;
    }

    int flags = 0,error = 0;
    socklen_t len;
    flags = fcntl( client_socket_, F_GETFL, 0);
    fcntl(client_socket_, F_SETFL, flags | O_NONBLOCK);
	
    server_addr.sin_port = htons( port_ );
    socklen_t server_addr_length = sizeof(server_addr);
 
    if ( (sock_ret = connect(client_socket_, (struct sockaddr*)&server_addr, server_addr_length)) < 0)
    {
        if ( errno != EINPROGRESS )
            return -1;
    }
    if( sock_ret == 0 )
    {
        LOGV(LL_INFO, "local connect To %s:%d ok!\n", addr_.c_str() , port_ );
    }

    FD_ZERO(&rdfds);
    FD_SET( client_socket_ , &rdfds);
    wdfds = rdfds;
    struct timeval tv;
    tv.tv_sec = 5;
    tv.tv_usec = 0;

    sock_ret = select( client_socket_ + 1, &rdfds, &wdfds , NULL, &tv);
    if ( sock_ret <= 0 )
    {
        LOGV(LL_ERROR, "select error  or timeout !");
        close(client_socket_);
        return -1;
    }
    if (/* FD_ISSET(client_socket_, &rdfds) ||*/ FD_ISSET(client_socket_, &wdfds) )
    {
        if ( FD_ISSET(client_socket_, &rdfds) )
        {
            LOGV(LL_ERROR, "socket refuse !");
            close(client_socket_);
            return -1;
        }
        if ( getsockopt(client_socket_, SOL_SOCKET, SO_ERROR, &error, &len) != 0)
        {
            if ( error )
            {
                LOGV(LL_ERROR, "Can not connect To %s:%d refuse,error %d msg %s !\n", 
                     addr_.c_str() , port_, error, strerror(error) );
                return -4;
            }
        }
        else 
        {
            LOGV(LL_INFO, "connect To %s:%d ok!\n", addr_.c_str() , port_ );
            connect_status_ = 1;
        }
    }else
    {
        LOGV(LL_ERROR, "Can not connect To %s:%d timeout!\n", addr_.c_str() , port_ );
        close(client_socket_);
        return -1;
    }

    printf("trace %d %d\n", client_socket_, sock_ret );
    fcntl(client_socket_, F_SETFL, flags);
 
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值