How to realize chat Mini Program on C++
Today, the editor will share with you the relevant knowledge points about how C++ can achieve chat Mini Program. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article, let's take a look at it.
The technologies involved:
C++ network programming
C++ multithreading
C++ STL
Design principle
Store the client in the form of a structure, access the existing client with vector, and turn on the multithreading logic
The server allows you to log in to multiple clients, public screen chat and private chat. By default, it belongs to public screen chat. If you want to chat in private, the format is "@ user name + message to be sent". The running effect is as follows:
Server implementation # include "stdafx.h" # include # include "windows.h" / / be sure to include the header file # include "process.h" # include # include # include using namespace std;#pragma comment (lib, "WS2_32.lib") / / shows that loading ws2_32.dll ws2_32.dll is the latest socket version int g_curPlayerNum = 0 / / current number of connections const char*g_PlayerName [] = / / assumed chatter name {"aaaa", "bbbb", "cccc", "dddd",}; struct PlayerInfo / / client using structure to store connected client {SOCKET sock; string name;}; vectorg_clientSockList; / / using vector to access connected client void process (void*param) {int index = * (int*) param / / current child thread number while (1) {/ / Server receives information / / int index = * (int*) param; char buf [2048] = {0}; / / receive buffer int bytes; if ((bytes = recv (gclientSockList [index] .sock, buf, sizeof (buf), 0) = SOCKET_ERROR) {cout sock, buf, strlen (buf), 0) = SOCKET_ERROR) {cout