cocos2d-x多线程解析域名

匿名 (未验证) 提交于 2019-12-02 23:49:02
原文链接:http://www.cnblogs.com/sealme/p/3771384.html

void * CNetThread::getIpAddress(void *arg)

{

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

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

登入账号");

struct addrinfo *infop = NULL, hint;

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

ai_family = AF_INET;

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);

if(ret)

EAI_AGAIN;

错误id %d", ret);

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(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

文章来源: https://blog.csdn.net/weixin_30632883/article/details/96587959
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!