当前位置:网站首页>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; }
边栏推荐
- ConstraintLayout的流式布局Flow
- MySQL character type learning notes
- 官网给的这个依赖是不是应该为flink-sql-connector-mysql-cdc啊,加了依赖调
- RMS to EAP is simply implemented through mqtt
- [paper reading] kgat: knowledge graph attention network for recommendation
- 非技术部门,如何参与 DevOps?
- 请问大佬们 有遇到过flink cdc mongdb 执行flinksql 遇到这样的问题的么?
- Solution of ellipsis when pytorch outputs tensor (output tensor completely)
- Window下线程与线程同步总结
- Learning Note 6 - satellite positioning technology (Part 1)
猜你喜欢
How did automated specification inspection software develop?
Design and Simulation of fuzzy PID control system for liquid level of double tank (matlab/simulink)
C语言实现QQ聊天室小项目 [完整源码]
Learning notes 5 - high precision map solution
Timed disappearance pop-up
WorkManager學習一
B站大量虚拟主播被集体强制退款:收入蒸发,还倒欠B站;乔布斯被追授美国总统自由勋章;Grafana 9 发布|极客头条...
Universal double button or single button pop-up
What is the most suitable book for programmers to engage in open source?
Idea create a new sprintboot project
随机推荐
Advanced opencv:bgr pixel intensity map
手机厂商“互卷”之年:“机海战术”失灵,“慢节奏”打法崛起
程序员如何活成自己喜欢的模样?
Tianlong Babu TLBB series - about items dropped from packages
《微信小程序-基础篇》小程序中的事件与冒泡
[paper reading] ckan: collaborative knowledge aware autonomous network for adviser systems
【观察】跨境电商“独立站”模式崛起,如何抓住下一个红利爆发时代?
微信小程序触底加载与下拉刷新的实现
【 conseils 】 obtenir les valeurs des axes X et y de la fonction cdfplot dans MATLAB
学习笔记5--高精地图解决方案
QT VT100 parser
Redis如何实现多可用区?
Lepton 无损压缩原理及性能分析
微信小程序中,从一个页面跳转到另一个页面后,在返回后发现页面同步滚动了
自动化规范检查软件如何发展而来?
Shortcut keys for vscode
横向滚动的RecycleView一屏显示五个半,低于五个平均分布
NCP1342芯片替代料PN8213 65W氮化镓充电器方案
The horizontally scrolling recycleview displays five and a half on one screen, lower than the average distribution of five
Workmanager Learning one