In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge about "C++ SOCKET multithreading how to realize chat Mini programs." In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
TCP/IP Protocol and SOCKET
What is Network Protocol?
In computer networks, data exchange between entities must follow pre-agreed rules, which are called protocols.
The components of the network protocol are:
1. The structure or format of syntax, data, and control information.
2. Semantics: what control information needs to be sent, what actions need to be performed, and what responses need to be made
3. Chronology: A detailed description of the order in which events are realized
In a network protocol, the same hierarchy of communicating entities must implement the same protocol, which is the principle of protocol equivalence.
TCP/IP Architecture and SOCKET
The details of TCP/IP architecture will not be discussed in this article, but if you don't have this knowledge and want to understand it quickly, you can compare network communication to a letter between two people. Your letter is the message or data to be transmitted in the communication process, and the network protocol wraps your "letter", such as affixing a stamp to you, wrapping an envelope, and dropping it into the mailbox. Then your "letter" can be sent 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 "gate" through which the sending process pushes the message; the message is pushed out using the underlying communication infrastructure to the "gate" where the receiving process resides; and the receiving process pulls the message in through the "gate." Sockets SOCKET is divided into datagram sockets and streaming sockets, using UDP protocol and TCP protocol respectively.
SOCKET programming
We try to write a unicast chat room that allows multiple clients to connect to the server, and unicast means that each client can only communicate with the server individually, and different clients cannot communicate with each other. To achieve this goal we also need to use multithreading. The overall implementation idea is as follows:
Not much to say, code.
Server side
#include "stdafx.h"#include#include#pragma comment (lib, "ws2_32.lib")using namespace std;const int PORT = 8000;#define IP "127.0.0.1"#define MaxClient10//The maximum number of clients that can be online at the same time can be modified at will #define MaxBufSize 1024int num =0;//The number of clients counter #define _CRT_SECURE_NO_WARINGS//Service Thread DWORD WINAPI SeverThread(LPVOID lpParameter){ //Create a new SOCKET for communication SOCKET *ClientSocket = (SOCKET*)lpParameter; int reciByt = 0; char RecvBuf[MaxBufSize]; char SendBuf[MaxBufSize]; char exitBuf[5]; //Start receiving while (1) {reciByt = recv(*ClientSocket, RecvBuf, sizeof(RecvBuf), 0); if (reciByt> 0) { //Close the connection when the message from the client is "exit" if (strlen(RecvBuf)==4) { for (int i = 0; i < 5; i++) { exitBuf[i] = RecvBuf[i]; } int flag = strcmp(exitBuf, "exit"); if (flag==0)//exit message received { 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.