Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to realize a multi-person chat room on C++

Shulou Source: shulou.com Published: 2022-05-31 13:57:19 10月03日 Update

本篇内容介绍了"C++如何实现多人聊天室"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

UDP

服务端代码:

// Test_Console.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include #include #include #include #include #include using namespace std;#pragma region 全局变量SOCKET server; // 服务端套接字sockaddr_in sai_server; // 服务端信息(ip、端口)// 消息格式struct umsg { int type; // 协议(1:加入 2:退出 3:发消息) char name[64]; // 用户名字 char text[512]; // 文本信息};// 客户端链表typedef struct ucnode { sockaddr_in addr; // 客户端的地址和端口号 umsg msg; // 客户端传来的消息 ucnode* next;} *ucnode_t;#pragma endregion#pragma region 依赖函数// 链表插入数据ucnode* insertNode(ucnode* head, sockaddr_in addr,umsg msg) { ucnode* newNode = new ucnode(); newNode->addr = addr; newNode->msg = msg; ucnode* p = head; if (p == nullptr) { head = newNode; } else { while (p->next != nullptr) { p = p->next; } p->next = newNode; } return head;}// 链表删除数据ucnode* deleteNode(ucnode* head, umsg msg) { ucnode* p = head; if (p == nullptr) { return head; } if (strcmp(p->msg.name, msg.name) == 0){ head = p->next; delete p; return head; } while (p->next != nullptr && strcmp(p->next->msg.name, msg.name) != 0) { p = p->next; } if (p->next == nullptr) { return head; } ucnode* deleteNode = p->next; p->next = deleteNode->next; delete deleteNode; return head;}#pragma endregionint main(){ cout

Tags: Message customer socket client service information address function server login port user size more data method behavior code global variable Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Xiaomi Shulou Tech Info Apple vpn