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

Socke基类:

包括Socket的初始化,主要是windows上,在android上就不需要了

如果平台为windows,则需要初始化

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. SocketBase::SocketBase()  
  2. {  
  3. _bInitSuccess = false;  
  4. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)  
  5. WORD wVersionRequested;  
  6. wVersionRequested = MAKEWORD(2, 0);  
  7. WSADATA wsaData;  
  8. int nRet = WSAStartup(wVersionRequested, &wsaData);  
  9. if (nRet != 0)   
  10. {  
  11. fprintf(stderr, "Initilize Error!\n");  
  12. return;  
  13. }  
  14. _bInitSuccess = true;  
  15. #endif  
  16.    
  17. }  


当然析构时也要释放资源

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. SocketBase::~SocketBase()  
  2. {  
  3. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)  
  4. if (_bInitSuccess)  
  5. {  
  6. WSACleanup();  
  7. }  
  8. #endif   
  9. }  

因为windows的socket()返回的socket句柄为SOCKET(UINT_PTR)

与android的socket()返回的socket句柄int, 类型不一样,所以都定义为HSocket

对于服务端和客户端都有关闭连接,所以基类就实现共同的。

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. void SocketBase::closeConnect(HSocket socket)  
  2. {  
  3. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)  
  4. close(socket);  
  5. #elif (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)  
  6. closesocket(socket);  
  7. #endif  
  8. }  

当执行socket()出错时,windows返回SOCKET_ERROR, android 返回<0,

所以实现error()

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. bool SocketBase::error(HSocket socket)  
  2. {  
  3. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)  
  4. return socket == SOCKET_ERROR;  
  5. #elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)  
  6. return socket < 0;  
  7. #endif  
  8. }  

SocketBase.h

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #ifndef __SOCKET_BASE_H__  
  2. #define __SOCKET_BASE_H__  
  3. #include "cocos2d.h"  
  4. #include <list>  
  5. #include <thread>  
  6. USING_NS_CC;  
  7. // 对于windows平台  
  8. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)  
  9. #include <WinSock2.h>  
  10. #pragma comment(lib, "WS2_32.lib")  
  11. #define HSocket SOCKET  
  12. // 对于android平台  
  13. #elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)  
  14. #include <arpa/inet.h>    // for inet_**  
  15. #include <netdb.h>    // for gethost**  
  16. #include <netinet/in.h>   // for sockaddr_in  
  17. #include <sys/types.h>    // for socket  
  18. #include <sys/socket.h>   // for socket  
  19. #include <unistd.h>  
  20. #include <stdio.h>         // for printf  
  21. #include <stdlib.h>   // for exit  
  22. #include <string.h>   // for bzero  
  23. #define HSocket int  
  24. #endif   
  25.    
  26. class SocketBase : public Ref  
  27. {  
  28. public:  
  29. SocketBase();  
  30. ~SocketBase();  
  31.    
  32. protected:  
  33. void closeConnect(HSocket socket);  
  34. bool error(HSocket socket);  
  35.    
  36. protected:  
  37. std::mutex _mutex;  
  38.    
  39. private:  
  40. bool _bInitSuccess;  
  41. };  
  42.    
  43. #endif  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值