Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does C++ realize chat Mini Program

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how C++ realizes chat Mini Program". In daily operation, I believe many people have doubts about how C++ can achieve chat Mini Program. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how C++ can realize chat Mini Program". Next, please follow the editor to study!

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report