服务端:
初始化服务端
向指定客户端发送消息
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
- #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