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 the chat room function of C++ based on socket UDP network programming

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article Xiaobian for everyone to introduce in detail "C++ based socket UDP network programming how to achieve chat room function", detailed content, clear steps, details properly handled, I hope this article "C++ based socket UDP network programming how to achieve chat room function" article can help you solve doubts, the following with Xiaobian's ideas slowly in-depth, together to learn new knowledge bar.

0. Flow chart of communication steps

(Left: Server; Right: Client)

1. server code

1.1 Server class header file (CServer_UDP.h)

#pragma once #include class CServer_UDP{public: CServer_UDP(); void SendMsg(const char sendBuf[]); void RecMsg(); ~CServer_UDP(); private: SOCKET m_sServer; struct sockaddr_in m_SocAddrClient; //Used to save client information bool m_terminal;} when establishing a connection;

1.2 Server class source file (CServer_UDP.cpp)

#define _WINSOCK_DEPRECATED_NO_WARNINGS#include "CServer_UDP.h" #include #pragma comment(lib, "ws2_32.lib") CServer_UDP::CServer_UDP():m_terminal(false){ //must be initialized as follows, otherwise socket() returns 10093 error//initialize WSA WORD sockVersion = MAKEWORD(2, 2); WSADATA wsaData; if (WSAStartup(sockVersion, &wsaData) != 0)//initialize ws2_32.dll { std::cout} via a process

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

Development

Wechat

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

12
Report