void* TigerThread::thread_funcation(void *arg) { ODSocket *pSocket = ODSocket::sharedODSocket(); pSocket->Init(); pSocket->Create(AF_INET, SOCK_STREAM,0); /* ///////////////////////////////////////////// char ipaddr[20]; cSocket.DnsParse("www.baidu.com", ipaddr); //192.168.22.154 //aliyun-027.btcsky.com /////////////////////////////////////////////--DNS解析 www->ip */ if (pSocket->Connect("58.83.190.220", 6621)) { char recvBuf[1024] = "\0"; // std::string testmsg= |login|[email protected],e10adc3949ba59abbe56e057f20f883e,68:A8:6D:10:66:DC|$\r\n std::string testmsg= pSocket->msg_To_Sever; pSocket->Send(testmsg.data(),testmsg.length(),0); pSocket->Recv(recvBuf,1024,0); std::string rec_msg=std::string(recvBuf); printf("%s",rec_msg.c_str()); } else { CCLog("服务器连接失败"); } pSocket->Close(); pSocket->Clean(); return NULL; }
void connectSocket(ODSocket &mysocket){ mysocket.Init(); mysocket.Create(AF_INET,SOCK_STREAM,0); mysocket.Bind(7880); mysocket.Listen(500); //mysocket.setMode(1); }
void netSocketManger::connectSocket() { if (!isConnect) { cSocket.Init(); cSocket.Create(AF_INET,SOCK_STREAM,0); if (m_sendEvent == NULL) { m_sendEvent = new CEvent(); } s_tea = 0; threadSend = std::thread(&netSocketManger::sendThread,this);//创建一个分支线程,回调到sendThread函数里 threadSend.detach(); } log("send thread is run"); }
void* SocketThread::start_thread(void *arg) { SocketThread* thred=(SocketThread*)arg; ODSocket cdSocket; cdSocket.Init(); bool isok=cdSocket.Create(AF_INET,SOCK_STREAM,0); // bool iscon=cdSocket.Connect("127.0.0.1",8000); bool iscon=cdSocket.Connect("192.168.0.144",8000); if(iscon){ thred->state=0; ResPonseThread::GetInstance()->start();//// 启动响应参数 CCLOG("conection"); }else{ thred->state=1; } thred->csocket=cdSocket; return NULL; }
bool connectToServer() { if (g_NetworkStat != NetworkStat_NotConnect) return true; if (cSocket.Init() == -1) { CCLog("\n=============================================================="); CCLog("ODSocket: connectToServer Init error,errorCode = %d!", cSocket.GetError()); CCLog("==============================================================\n"); // 通知链接失败 GlobalNetworkMsgDispacher::getMsgDispacher()->addPakage(new GlobalNetPackage(Type_Conncet_Err,NULL,NULL)); return false; } if (cSocket.Create(AF_INET,SOCK_STREAM,0) == false) { CCLog("\n=============================================================="); CCLog("ODSocket: connectToServer Create error,errorCode = %d!",cSocket.GetError()); CCLog("==============================================================\n"); // 通知链接失败 GlobalNetworkMsgDispacher::getMsgDispacher()->addPakage(new GlobalNetPackage(Type_Conncet_Err,NULL,NULL)); return false; } if (cSocket.Connect(NetWorkThreadProc::getProc()->getCurrentIp(),NetWorkThreadProc::getProc()->getCurrentPort()) == false) { CCLog("\n=========================================================================="); CCLog("ODSocket: connectToServer[%s:%d] Connect error,errorCode = %d!",NetWorkThreadProc::getProc()->getCurrentIp(),NetWorkThreadProc::getProc()->getCurrentPort(),cSocket.GetError()); CCLog("============================================================================\n"); closeConnection(); // 通知链失败 GlobalNetworkMsgDispacher::getMsgDispacher()->addPakage(new GlobalNetPackage(Type_Conncet_Err,NULL,NULL)); return false; } CCLog("\n\n"); CCLog("\n=============================================================="); CCLog("ODSocket : connectToServer %s-%d Success!",NetWorkThreadProc::getProc()->getCurrentIp(),NetWorkThreadProc::getProc()->getCurrentPort()); CCLog("==============================================================\n"); CCLog("\n\n"); //cSocket.setT //设置状态 g_NetworkStat = NetworkStat_Connected; // 设置超时 { cSocket.SetRecvTimeOut(RECV_TIME_OUT); cSocket.SetSendTimeOut(SEND_TIME_OUT); } // 这个在init时已经调用了,这里不需要了,而且WSAStartup应该是winSocket第一个调用的函数 // 所以写在这里也是不对的 //#ifdef WIN32 // WORD wVersionRequested; // WSADATA wsaData; // int err; // // /* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */ // wVersionRequested = MAKEWORD(2, 2); // // err = WSAStartup(wVersionRequested, &wsaData); // if (err != 0) { // /* Tell the user that we could not find a usable */ // /* Winsock DLL. */ // CCLog("WSAStartup failed"); // // closeConnection(); // // return false; // } //#endif // 通知链接成功 GlobalNetworkMsgDispacher::getMsgDispacher()->addPakage(new GlobalNetPackage(Type_Conncet_Suc,NULL,NULL)); return true; }
void MapScene::clientR() { ODSocket cSocketR; cSocketR.Init(); cSocketR.Create(AF_INET,SOCK_STREAM,0); while(!cSocketR.Connect(ip.c_str(),portW)) { #ifdef WIN32 Sleep(1000); #else usleep(1000 * 1000); #endif } //因为是client 所以用的是portR; char buf[BLOCK_SIZE]; for (;;) { if (!start) return ; int len = cSocketR.Recv(buf,BLOCK_SIZE ,0); if (len <= 0 ) { heartBeat = false; break; } if (!status) break; int pAngle; int x; int y; int avatarId; int type = 0; int number; bool flag[MAX][4][20]; int t_life = 0; int state = 0; avatarId = getIntFromStr(buf, 1); state = getIntFromStr(buf+1, 1); t_life = getIntFromStr(buf+2, 2); pAngle = getIntFromStr(buf+4, 4); x = getIntFromStr(buf+8, 4); y = getIntFromStr(buf+12, 4); avatar[avatarId]->pAngle = pAngle; avatar[avatarId]->getsprite()->setRotation(pAngle); avatar[avatarId]->getsprite()->setPosition(ccp(x,y)); if (!avatar[avatarId]->getsprite()->getIsVisible() && status) avatar[avatarId]->getsprite()->setIsVisible(true); len = 16; if (state == 1 && !player->isFrozen) { player->isFrozen = true; schedule(schedule_selector(MapScene::freezeplayer), 0.1f); schedule(schedule_selector(MapScene::revertplayer), 2.0f); } if (t_life < player->life) { if (player->life == t_life + 5) { schedule(schedule_selector(MapScene::attackByBomb),0.1f); } reduceLife(t_life); player->life = t_life; schedule(schedule_selector(MapScene::syncLife), 0.1f); } for (int i = 0 ; i < MAX ; ++i) for (int j = 0 ; j <= 2; ++j) for (int k = 0 ; k < avatarBullets[i][j]->capacity(); ++k) flag[i][j][k] = false; while(1) { type = getIntFromStr(buf+len, 1); if (type == 9) break; len += 1; number = getIntFromStr(buf+len, 2); len += 2; pAngle = getIntFromStr(buf+len, 4); len += 4; x = getIntFromStr(buf+len, 4); len += 4; y = getIntFromStr(buf+len, 4); len += 4; GameBullet * mybullet = (GameBullet *)(avatarBullets[avatarId][type]->objectAtIndex(number)); mybullet->vAngle = pAngle; mybullet->getsprite()->setRotation(pAngle); mybullet->getsprite()->setPosition(ccp(x,y)); if (!mybullet->getsprite()->getIsVisible()) mybullet->getsprite()->setIsVisible(true); flag[avatarId][type][number] = true; } for (int i = 0 ; i < MAX ; ++i) for (int j = 0 ; j <= 2; ++j) for (int k = 0 ; k < avatarBullets[i][j]->capacity(); ++k) { if (!flag[i][j][k]) { ((GameBullet *)(avatarBullets[i][j]->objectAtIndex(k)))->getsprite()->setIsVisible(false); } } if (t_life == 0) break; } cSocketR.Close(); cSocketR.Clean(); }
void MapScene::clientW() { ODSocket cSocketW; cSocketW.Init(); cSocketW.Create(AF_INET,SOCK_STREAM,0); while(!cSocketW.Connect(ip.c_str(),portR)) { #ifdef WIN32 Sleep(1000); #else usleep(1000 * 1000); #endif } ; //因为是client 所以用的是portR; char buf[BLOCK_SIZE]; sprintf(buf,"%1d",id); int aid = -1; int state = 0; while(1) { if (!start) break; for(int i = 0 ; i < MAX; ++i) { if (avatar[i]->getsprite()->getIsVisible()) { aid = i; break; } } state = 0; if(avatar[aid]->isFrozen) { state = 1; } sprintf(buf+1,"%1d",state); sprintf(buf+2,"%2d",avatarLife); CCPoint herop = (player->getsprite())->getPosition(); int dy = herop.y; int dx = herop.x; int angle = (int)player->pAngle; sprintf(buf+4, "%4d", angle); sprintf(buf+8, "%4d", dx); sprintf(buf+12, "%4d", dy); int len = 16; for (int i = 0; i <= 2; ++i) for (int j = 0; j < bullets[i]->capacity(); ++j) { GameBullet * mybullet = (GameBullet *)(bullets[i]->objectAtIndex(j)); if (mybullet->getsprite()->getIsVisible()) { CCPoint p = (mybullet->getsprite()->getPosition()); sprintf(buf+len, "%1d", (int)i); len += 1; sprintf(buf+len, "%2d", (int)j); len += 2; sprintf(buf+len, "%4d", (int)mybullet->vAngle); len += 4; sprintf(buf+len, "%4d", (int)p.x); len += 4; sprintf(buf+len, "%4d", (int)p.y); len += 4; } } buf[len] = '9'; //if (demo > 3 || temp != 0) //{ if (cSocketW.Send(buf, BLOCK_SIZE ,0)== -1) break; // } #ifdef WIN32 Sleep(30); #else usleep(30 * 1000); #endif } cSocketW.Close(); cSocketW.Clean(); }