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