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

Simple version of UDP chat system

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

Model diagram with data storage

The overall framework

In server

Udp_server.h

The add_user function is used to add new online user information.

1 # pragma once 2 # include 3 # include 4 using namespace std; 5 # include "data_pool.h" 6 # include 7 # include 8 class udp_server 9 {10 public: 11 udp_server (const std::string & ip= "192.168.137.129", const short & port=808 0); 12 ~ udp_server (); 13 void init (); 14 int recv_data (); 15 int send_data (struct sockaddr_in& client,socklen_t len,string& msg) 16 int broadcast (); 17 protected: 18 void add_user (string& key_ip,struct sockaddr_in& client); 19 protected: 20 string _ ip; 21 short _ port; 22 int _ sock; 23 std::map _ online_user; 24 pool_data _ pool_data; 25}

Udp_server.cpp

1 # include "udp_server.h" 2 # include "comm.h" 3 # include "data_pool.h" 4 5 udp_server::udp_server (const std::string & default_ip,const short & default_port) 6: _ ip (default_ip) 7, _ port (default_port) 8 _ sock (- 1) 9 {} 10 11 udp_server::~udp_server () 12 {13 if (_ sock > 0) 14 close (_ sock) 15} 16 17 void udp_server::init () 18 {19 _ sock=socket (AF_INET,SOCK_DGRAM,0); 20 if (_ sock)

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

Network Security

Wechat

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

12
Report