当前位置:网站首页>C language QQ chat room small project [complete source code]
C language QQ chat room small project [complete source code]
2022-07-05 10:27:00 【Chenziqing: see】
Chat items are used to practice Windows Under the tcp socket Programming and thread synchronization , among send and recv Use , Yes tcp Chinese garbled code often encountered in data transmission 、 Problems such as incomplete data can be demonstrated and corrected .
Project renderings

Client code
#include <WinSock2.h> #include <iostream> #include <windows.h> #include <process.h> #include<WS2tcpip.h> #pragma comment(lib, "ws2_32.lib") #define NAME_SIZE 32 #define BUF_SIZE 256 char szName[NAME_SIZE] = "[DEFAULT]"; char szMsg[BUF_SIZE]; // Send a message to the server unsigned WINAPI SendMsg(void* arg) { //1 Receive the parameters passed in SOCKET hClntSock = *((SOCKET*)arg); char szNameMsg[NAME_SIZE + BUF_SIZE]; // Another name , News again // Cycle to receive messages from the console while (1) { fgets(szMsg, BUF_SIZE, stdin); // Blocked in this sentence // Exit mechanism When I received q or Q sign out if (!strcmp(szMsg, "Q\n") || !strcmp(szMsg, "q\n")) { closesocket(hClntSock); exit(0); } sprintf(szNameMsg, "%s %s", szName, szMsg);// String splicing send(hClntSock, szNameMsg, strlen(szNameMsg), 0);// send out } return 0; } // Receiving messages from the server unsigned WINAPI RecvMsg(void* arg) { //1 Receive the parameters passed in SOCKET hClntSock = *((SOCKET*)arg); char szNameMsg[NAME_SIZE + BUF_SIZE]; // Another name , News again int iLen = 0; while (1) { //recv Blocking iLen = recv(hClntSock, szNameMsg, NAME_SIZE + BUF_SIZE - 1, 0); // The server is disconnected if (iLen == -1) { return -1; } // szNameMsg Of 0 To iLen -1 All the data received iLen individual szNameMsg[iLen] = 0; // The received data is output to the console fputs(szNameMsg, stdout); } return 0; } int main(int argc,char *argv[]) { WORD wVersionRequested; WSADATA wsaData; int err; SOCKET hSock; SOCKADDR_IN servAdr; HANDLE hSendThread, hRecvThread; wVersionRequested = MAKEWORD(1, 1); // Initialize socket library err = WSAStartup(wVersionRequested, &wsaData); if (err != 0) { return err; } if (LOBYTE(wsaData.wVersion) != 1 || HIBYTE(wsaData.wVersion) != 1) { WSACleanup(); return -1; } sprintf(szName, "[%s]", argv[1]); //1 establish socket hSock = socket(PF_INET, SOCK_STREAM, 0); // 2 Configure ports and addresses memset(&servAdr, 0, sizeof(servAdr)); inet_pton(AF_INET, "192.168.10.3", &servAdr.sin_addr); //servAdr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); servAdr.sin_family = AF_INET; servAdr.sin_port = htons(9999); // 3 Connect to server if (connect(hSock, (SOCKADDR*)&servAdr, sizeof(servAdr)) == SOCKET_ERROR) { printf("connect error error code = %d\n", GetLastError()); return -1; } hSendThread = (HANDLE)_beginthreadex(NULL, 0, SendMsg, (void*)&hSock, 0, NULL); // 5 Receive messages to the server Arrange a worker Start a thread to receive messages hRecvThread = (HANDLE)_beginthreadex(NULL, 0, RecvMsg, (void*)&hSock, 0, NULL); // Wait for the signal of the kernel object to change WaitForSingleObject(hSendThread, INFINITE); WaitForSingleObject(hRecvThread, INFINITE); // 6 Close socket closesocket(hSock); WSACleanup(); return 0; }
Server code
#include<stdio.h> #include<windows.h> #include<process.h> #pragma comment(lib, "ws2_32.lib") #define MAX_CLNT 256 #define MAX_BUF_SIZE 256 SOCKET clntSocks[MAX_CLNT]; // All connected clients socket HANDLE hMutex; int clntCnt = 0; // Number of current connections void SendMsg(char* szMsg, int iLen) { int i = 0; WaitForSingleObject(hMutex, INFINITE); for (i = 0; i < clntCnt; i++) { send(clntSocks[i], szMsg, iLen, 0); } ReleaseMutex(hMutex); } unsigned WINAPI HandleCln(void* arg) { SOCKET hClntSock = *((SOCKET*)arg); int iLen = 0, i; char szMsg[MAX_BUF_SIZE] = { 0 }; while (1) { iLen = recv(hClntSock, szMsg, sizeof(szMsg), 0); if (iLen != -1) { // The received data is immediately sent to all clients SendMsg(szMsg, iLen); } else { break; } } printf(" At this time, the number of connections is %d\n", clntCnt); //3 A connection is disconnected , Need to handle disconnected connections Traverse WaitForSingleObject(hMutex, INFINITE); for (i = 0; i < clntCnt; i++) { if (hClntSock == clntSocks[i]) { // displacement while (i++ < clntCnt) { clntSocks[i] = clntSocks[i + 1]; } break; } } clntCnt--; // A self subtraction of the current number of connections printf(" Disconnect the number of connections at this time %d", clntCnt); ReleaseMutex(hMutex); closesocket(hClntSock); return 0; } int main() { // Load socket library WORD wVersionRequested; WSADATA wsaData; int err; HANDLE hThread; wVersionRequested = MAKEWORD(1, 1); // Initialize socket library err = WSAStartup(wVersionRequested, &wsaData); if (err != 0) { return err; } if (LOBYTE(wsaData.wVersion) != 1 || HIBYTE(wsaData.wVersion) != 1) { WSACleanup(); return -1; } // Create a mutex hMutex = CreateMutex(NULL, FALSE, NULL); // New socket SOCKET sockSrv = socket(AF_INET, SOCK_STREAM, 0); SOCKADDR_IN addrSrv; addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY); addrSrv.sin_family = AF_INET; addrSrv.sin_port = htons(9999); // Bind socket to local IP Address , Port number 9190 if (bind(sockSrv, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR)) == SOCKET_ERROR) { printf("bind ERRORnum = %d\n", GetLastError()); return -1; } // Start listening if (listen(sockSrv, 5) == SOCKET_ERROR) { printf("listen ERRORnum = %d\n", GetLastError()); return -1; } printf("start listen\n"); SOCKADDR_IN addrCli; int len = sizeof(SOCKADDR); while (1) { // Receive customer connections sockConn At this time, the client connection SOCKET sockConn = accept(sockSrv, (SOCKADDR*)&addrCli, &len); WaitForSingleObject(hMutex, INFINITE); clntSocks[clntCnt++] = sockConn; ReleaseMutex(hMutex); hThread = (HANDLE)_beginthreadex(NULL, 0, HandleCln, (void*)&sockConn, 0, NULL); printf("Connect client IP: %s \n", inet_ntoa(addrCli.sin_addr)); printf("Connect client num: %d \n", clntCnt); } closesocket(sockSrv); WSACleanup(); return 0; }
边栏推荐
- Uni app running to wechat development tool cannot Preview
- 钉钉、企微、飞书学会赚钱了吗?
- Cerebral Cortex:有向脑连接识别帕金森病中广泛存在的功能网络异常
- Activity enter exit animation
- QT VT100 parser
- [论文阅读] KGAT: Knowledge Graph Attention Network for Recommendation
- Write double click event
- How to plan the career of a programmer?
- @Serializedname annotation use
- TypeError: Cannot read properties of undefined (reading ‘cancelToken‘)
猜你喜欢

非技术部门,如何参与 DevOps?

程序员搞开源,读什么书最合适?

Learning note 4 -- Key Technologies of high-precision map (Part 2)

Cerebral cortex: directed brain connection recognition widespread functional network abnormalities in Parkinson's disease

Usage differences between isempty and isblank

AtCoder Beginner Contest 258「ABCDEFG」

自动化规范检查软件如何发展而来?

What is the most suitable book for programmers to engage in open source?

ConstraintLayout官方提供圆角ImageFilterView

【观察】跨境电商“独立站”模式崛起,如何抓住下一个红利爆发时代?
随机推荐
Write double click event
How do programmers live as they like?
Have the bosses ever encountered such problems in the implementation of flinksql by Flink CDC mongdb?
Using directive in angualr2 to realize that the picture size changes with the window size
【黑马早报】罗永浩回应调侃东方甄选;董卿丈夫密春雷被执行超7亿;吉利正式收购魅族;华为发布问界M7;豆瓣为周杰伦专辑提前开分道歉...
NCP1342芯片替代料PN8213 65W氮化镓充电器方案
[paper reading] ckan: collaborative knowledge aware autonomous network for adviser systems
[tips] get the x-axis and y-axis values of cdfplot function in MATLAB
天龙八部TLBB系列 - 关于技能冷却和攻击范围数量的问题
学习笔记5--高精地图解决方案
Tianlong Babu TLBB series - single skill group injury
Applet image height adaptation and setting text line height
Cerebral cortex: directed brain connection recognition widespread functional network abnormalities in Parkinson's disease
手机厂商“互卷”之年:“机海战术”失灵,“慢节奏”打法崛起
驱动制造业产业升级新思路的领域知识网络,什么来头?
《天天数学》连载58:二月二十七日
php解决redis的缓存雪崩,缓存穿透,缓存击穿的问题
La vue latérale du cycle affiche cinq demi - écrans en dessous de cinq distributions moyennes
Unity particle special effects series - the poison spray preform is ready, and the unitypackage package is directly used - on
How did automated specification inspection software develop?