服务端:
初始化服务端
向指定客户端发送消息
voIDsendMessage(HSocketsocket,constchar*data,255); Font-size:9.5pt; Font-family:新宋体">intcount);
向所有客户端发送消息
voID sendMessage(const char* data,int count);
当服务端开启后的回调函数
std::function<voID(constchar*ip)>onStart;
当有新连接时的回调函数
voID(HSocketsocket)>onNewConnection;
当有消息时的回调函数
std::function<voID(intcount)>onRecv;
当有客户端断开连接时的回调函数
HSocketsocket)>ondisconnect;
SocketServer.h
[cpp] view plain copy #ifndef__SOCKET_SERVER_H__ #define__SOCKET_SERVER_H__ #include"SocketBase.h" classSocketServer:publicSocketBase { public: staticSocketServer*create(); SocketServer(); ~SocketServer(); boolstartServer(); voIDsendMessage(HSocketsocket,constchar*data,intcount); voIDsendMessage(intcount); std::function<voID(char*ip)>onStart; std::function<voID(HSocketsocket)>onNewConnection; intcount)>onRecv; voID(HSocketsocket)>ondisconnect; private: boolinitServer(); voIDacceptClIEnt(); voIDacceptFunc(); voIDnewClIEntConnected(HSocketsocket); voIDrecvMessage(HSocketsocket); private: HSocket_socketServer; std::List<HSocket>_clIEntSockets; }; #endif
SocketServer.cpp
#include"SocketServer.h" SocketServer*SocketServer::create() { autoserver=newSocketServer; returnserver; } SocketServer::SocketServer(): _socketServer(0), onRecv(nullptr), onStart(nullptr),248)"> onNewConnection(nullptr) } SocketServer::~SocketServer() _clIEntSockets.clear(); if(_socketServer) this->closeConnect(_socketServer); }; boolSocketServer::startServer() if(!initServer()) returnfalse; true; boolSocketServer::initServer() if(_socketServer!=0) _socketServer=socket(AF_INET,SOCK_STREAM,0); if(error(_socketServer)) log("socketerror!"); _socketServer=0; false; do structsockaddr_insockAddr; memset(&sockAddr,sizeof(sockAddr)); sockAddr.sin_family=AF_INET; sockAddr.sin_port=htons(8000); sockAddr.sin_addr.s_addr=htonl(INADDR_ANY); intret=0; ret=bind(_socketServer,(constsockaddr*)&sockAddr,153); Font-weight:bold; background-color:inherit">if(ret<0) log("binderror!"); break; ret=Listen(_socketServer,5); if(ret<0) log("Listenerror!"); break; //start charhostname[256]; gethostname(hostname,153); Font-weight:bold; background-color:inherit">sizeof(hostname)); structhostent*hostInfo=gethostbyname(hostname); char*ip=inet_ntoa(*(structin_addr*)*hostInfo->h_addr_List); this->acceptClIEnt(); if(onStart!=nullptr) log("startserver!"); onStart(ip); true; }while(false); _socketServer=0; voIDSocketServer::acceptClIEnt() std::threadth(&SocketServer::acceptFunc,153); Font-weight:bold; background-color:inherit">this); th.detach(); voIDSocketServer::acceptFunc() intlen=sizeof(sockaddr); structsockaddr_insockAddr; true) HSocketclIEntSock=accept(_socketServer,(sockaddr*)&sockAddr,&len); if(error(clIEntSock)) log("accepterror!"); this->newClIEntConnected(clIEntSock); voIDSocketServer::newClIEntConnected(HSocketsocket) log("newconnect!"); _clIEntSockets.push_back(socket); threadth(&SocketServer::recvMessage,153); Font-weight:bold; background-color:inherit">this,socket); if(onNewConnection!=nullptr) onNewConnection(socket); voIDSocketServer::recvMessage(HSocketsocket) charbuff[1024]; ret=recv(socket,buff,153); Font-weight:bold; background-color:inherit">sizeof(buff),0); log("recv(%d)error!",socket); _mutex.lock(); this->closeConnect(socket); _clIEntSockets.remove(socket); if(ondisconnect!=nullptr) ondisconnect(socket); _mutex.unlock(); else buff[ret]=0; log("recvmsg:%s",buff); if(ret>0&&onRecv!=nullptr) onRecv(buff,ret); voIDSocketServer::sendMessage(HSocketsocket,87); Font-weight:bold; background-color:inherit">intcount) for(auto&sock:_clIEntSockets) if(sock==socket) intret=send(socket,data,count,0); log("senderror!"); voIDSocketServer::sendMessage(intcount) for(auto&socket:_clIEntSockets) log("senderror!"); } 总结
以上是内存溢出为你收集整理的cocos2dx3.x利用sokcet创建客户端和服务端 (二)全部内容,希望文章能够帮你解决cocos2dx3.x利用sokcet创建客户端和服务端 (二)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)