博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
cocos2d-x多线程解析域名
阅读量:4546 次
发布时间:2019-06-08

本文共 1592 字,大约阅读时间需要 5 分钟。

void * CNetThread::getIpAddress(void *arg)

{

    //将原先的会引发线程不安全的代码更改为线程安全的代码

    CServerInfo *info = static_cast<CServerInfo *>(arg);

    //    CServerInfo info;

    //    GameInfo::Instance()->getServerInfo(info, index);

    

    //    CCLOG("登入账号");

    struct addrinfo *infop = NULL, hint;

    memset(&hint, 0, sizeof(hint));

    hint.ai_family = AF_INET;

    hint.ai_socktype = SOCK_STREAM;

    char buf[64] = {};

    sprintf(buf, "%d", info->m_PORT);

    CCLOG( "getaddrinfo addr:%s port:%s", info->m_IP.c_str(), buf );

    int ret = getaddrinfo(info->m_IP.c_str(), buf, &hint, &infop);

    //    sprintf(buf, "%d", info.m_PORT);

    //    int ret = getaddrinfo(info.m_IP.c_str(), buf, &hint, &infop);

    if(ret)

    {

        EAI_AGAIN;

        //        char res[256] = {0};

        //        sprintf(res, "getaddrinfo 错误id %d", ret);

        //        CCMessageBox(res, s_LocalError);

        //        GameInfo::Instance()->setAnalyzeIpSuccess(false);

        CCLOG( "getaddrinfo 错误id %d", ret );

    }

    else

    {

        struct sockaddr_in * sa = (struct sockaddr_in *)infop->ai_addr;

        string ipAddress = inet_ntoa(sa->sin_addr);

        int port = ntohs(sa->sin_port);

        //解析成功设置GameInfo中的标志位和ip信息

        GameInfo::Instance()->setIpAddress(ipAddress);

        GameInfo::Instance()->setPort(port);

        //        GameInfo::Instance()->setAnalyzeIpSuccess(true);

    }

    GameInfo::Instance()->setAnalyzeIpSuccess(ret);

    

    GameInfo::Instance()->setThreadFinish(true);

    delete info;

    returnNULL;

}

 

void CNetThread::createThread()

{

    CServerInfo * info = newCServerInfo();

    unsigned int index = GameInfo::Instance()->getServerSelect();

    GameInfo::Instance()->getServerInfo( *info, index );

    pthread_create( & m_tNetThreadLoad, NULL, CNetThread::getIpAddress, info );

}

 

转载于:https://www.cnblogs.com/sealme/p/3771384.html

你可能感兴趣的文章
Codeforces 1144G Two Merged Sequences dp
查看>>
STL内存分配方式
查看>>
NS2移动节点
查看>>
redis取值报错
查看>>
Oracle 客户端 使用 expdp/impdp 示例 说明
查看>>
模拟3d
查看>>
【BZOJ】 1041: [HAOI2008]圆上的整点
查看>>
Oracle Data Guard 重要配置参数
查看>>
c3p0参数解释
查看>>
ASP.NET MVC5+EF6+EasyUI 后台管理系统(41)-组织架构
查看>>
c++ 字符串转换
查看>>
Redis 补充
查看>>
iOS开发UI篇—UITableviewcell的性能优化和缓存机制
查看>>
第十五节:pandas之concat()级联
查看>>
.net中判断距离高考多长时间的js函数
查看>>
[HNOI2008]GT考试
查看>>
uva 437 The Tower of Babylon
查看>>
ubuntu 16.04 + python + matplotlib下画图显示中文设置
查看>>
SQL语句练习
查看>>
C#中不用安装Oracle客户端连接Oracle数据库(转)
查看>>