cocos2dx3.x利用sokcet创建客户端和服务端 (二)

前端之家收集整理的这篇文章主要介绍了cocos2dx3.x利用sokcet创建客户端和服务端 (二)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

服务端:

初始化服务端

向指定客户端发送消息

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

  1. #ifndef__SOCKET_SERVER_H__
  2. #define__SOCKET_SERVER_H__
  3. #include"SocketBase.h"
  4. classSocketServer:publicSocketBase
  5. {
  6. public:
  7. staticSocketServer*create();
  8. SocketServer();
  9. ~SocketServer();
  10. boolstartServer();
  11. voidsendMessage(HSocketsocket,constchar*data,intcount);
  12. voidsendMessage(intcount);
  13. std::function<void(char*ip)>onStart;
  14. std::function<void(HSocketsocket)>onNewConnection;
  15. intcount)>onRecv;
  16. void(HSocketsocket)>onDisconnect;
  17. private:
  18. boolinitServer();
  19. voidacceptClient();
  20. voidacceptFunc();
  21. voidnewClientConnected(HSocketsocket);
  22. voidrecvMessage(HSocketsocket);
  23. private:
  24. HSocket_socketServer;
  25. std::list<HSocket>_clientSockets;
  26. };
  27. #endif



SocketServer.cpp

    #include"SocketServer.h"
  1. SocketServer*SocketServer::create()
  2. {
  3. autoserver=newSocketServer;
  4. returnserver;
  5. }
  6. SocketServer::SocketServer():
  7. _socketServer(0),
  8. onRecv(nullptr),
  9. onStart(nullptr),248)"> onNewConnection(nullptr)
  10. }
  11. SocketServer::~SocketServer()
  12. _clientSockets.clear();
  13. if(_socketServer)
  14. this->closeConnect(_socketServer);
  15. };
  16. boolSocketServer::startServer()
  17. if(!initServer())
  18. returnfalse;
  19. true;
  20. boolSocketServer::initServer()
  21. if(_socketServer!=0)
  22. _socketServer=socket(AF_INET,SOCK_STREAM,0);
  23. if(error(_socketServer))
  24. log("socketerror!");
  25. _socketServer=0;
  26. false;
  27. do
  28. structsockaddr_insockAddr;
  29. memset(&sockAddr,sizeof(sockAddr));
  30. sockAddr.sin_family=AF_INET;
  31. sockAddr.sin_port=htons(8000);
  32. sockAddr.sin_addr.s_addr=htonl(INADDR_ANY);
  33. intret=0;
  34. ret=bind(_socketServer,(constsockaddr*)&sockAddr,153); font-weight:bold; background-color:inherit">if(ret<0)
  35. log("binderror!");
  36. break;
  37. ret=listen(_socketServer,5);
  38. if(ret<0)
  39. log("listenerror!");
  40. break;
  41. //start
  42. charhostName[256];
  43. gethostname(hostName,153); font-weight:bold; background-color:inherit">sizeof(hostName));
  44. structhostent*hostInfo=gethostbyname(hostName);
  45. char*ip=inet_ntoa(*(structin_addr*)*hostInfo->h_addr_list);
  46. this->acceptClient();
  47. if(onStart!=nullptr)
  48. log("startserver!");
  49. onStart(ip);
  50. true;
  51. }while(false);
  52. _socketServer=0;
  53. voidSocketServer::acceptClient()
  54. std::threadth(&SocketServer::acceptFunc,153); font-weight:bold; background-color:inherit">this);
  55. th.detach();
  56. voidSocketServer::acceptFunc()
  57. intlen=sizeof(sockaddr);
  58. structsockaddr_insockAddr;
  59. true)
  60. HSocketclientSock=accept(_socketServer,(sockaddr*)&sockAddr,&len);
  61. if(error(clientSock))
  62. log("accepterror!");
  63. this->newClientConnected(clientSock);
  64. voidSocketServer::newClientConnected(HSocketsocket)
  65. log("newconnect!");
  66. _clientSockets.push_back(socket);
  67. threadth(&SocketServer::recvMessage,153); font-weight:bold; background-color:inherit">this,socket);
  68. if(onNewConnection!=nullptr)
  69. onNewConnection(socket);
  70. voidSocketServer::recvMessage(HSocketsocket)
  71. charbuff[1024];
  72. ret=recv(socket,buff,153); font-weight:bold; background-color:inherit">sizeof(buff),0);
  73. log("recv(%d)error!",socket);
  74. _mutex.lock();
  75. this->closeConnect(socket);
  76. _clientSockets.remove(socket);
  77. if(onDisconnect!=nullptr)
  78. onDisconnect(socket);
  79. _mutex.unlock();
  80. else
  81. buff[ret]=0;
  82. log("recvmsg:%s",buff);
  83. if(ret>0&&onRecv!=nullptr)
  84. onRecv(buff,ret);
  85. voidSocketServer::sendMessage(HSocketsocket,87); font-weight:bold; background-color:inherit">intcount)
  86. for(auto&sock:_clientSockets)
  87. if(sock==socket)
  88. intret=send(socket,data,count,0);
  89. log("senderror!");
  90. voidSocketServer::sendMessage(intcount)
  91. for(auto&socket:_clientSockets)
  92. log("senderror!");
  93. }
原文链接:https://www.f2er.com/cocos2dx/345114.html

猜你在找的Cocos2d-x相关文章