Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

本篇内容介绍了"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

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report