Get the App
SLTechnology News&Howtos  ›  Network Security  › 

Simple version of UDP chat system

Shulou Source: shulou.com Published: 2022-06-01 04:50:43 10月04日 Update

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)

Tags: Function data message two information population framework model user thread processing storage broadcast version system Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux NVidia Shulou Tech Info Microsoft Apple