In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the C++ SOCKET multithreading how to achieve chat Mini Program related knowledge, the content is detailed and easy to understand, simple and fast operation, with a certain reference value, I believe that everyone after reading this C++ multithreading how to achieve chat Mini Program article will have a harvest, let's take a look at it.
TCP/IP Protocol and SOCKET
What is a network protocol?
In a computer network, the data exchange between entities must abide by the agreed rules, which are called protocols.
The constituent elements of a network protocol are:
1. The structure or format of syntax, data, and control information
two。 Semantics: what control information needs to be sent, what actions are done, and what responses are made.
3. Timing: a detailed description of the sequence of events
In a network protocol, the same hierarchical structure of the communicating entity must implement the same protocol, which is the principle of protocol equivalence.
TCP/IP Architecture and SOCKET
The details of the TCP/IP architecture are not discussed in this article, and if you don't have the knowledge to understand it quickly, you can compare network communication to writing letters between two people. Your letter is the message or data to be transmitted in the process of communication, and the network protocol wraps your "letter", such as putting a stamp on you, wrapping an envelope, and dropping it into the mailbox, and then your "letter" can be delivered to the recipient through the post office.
SOCKET (socket) is a typical network programming interface provided by TCP/IP network operating system for network program development. Processes send and receive messages through SOCKET. You can think of SOCKET as a "door". The sending process pushes the message out of the "door"; after the message is pushed out, it is delivered to the "door" of the receiving process using the lower-level communication facilities; and then the receiving process pulls the message in through the "door". Socket SOCKET is divided into Datagram socket and streaming socket, using UDP protocol and TCP protocol respectively.
SOCKET programming
We try to write a unicast chat room, which allows multiple clients to connect with the server, and unicast means that each client can only communicate with the server separately, and different clients cannot communicate with each other. In order to achieve this goal, we also need to use multithreading. The overall implementation idea is as follows:
Don't say much, put on the code.
Server end
# include "stdafx.h" # include#include#include#pragma comment (lib, "ws2_32.lib") using namespace std;const int PORT = 8000 positives define IP "127.0.0.1" # define MaxClient 10 binds / the maximum number of clients that can be online at the same time, please feel free to modify # define MaxBufSize 1024int num = 0 / / client count counter # define _ CRT_SECURE_NO_WARINGS// service thread DWORD WINAPI SeverThread (LPVOID lpParameter) {/ / create a new SOCKET for communication SOCKET* ClientSocket = (SOCKET*) lpParameter; int receByt = 0; char RecvBuf [MaxBufSize]; char SendBuf [MaxBufSize]; char exitBuf [5]; / / start receiving while (1) {receByt = recv (* ClientSocket, RecvBuf, sizeof (RecvBuf), 0) If (receByt > 0) {/ / when the message from the client is "exit", close the connection if (strlen (RecvBuf) = = 4) {for (int I = 0; I < 5; iTunes +) {exitBuf [I] = RecvBuf [I];} int flag= strcmp (exitBuf, "exit"); if (flag==0) / / receives the exit message {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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.