• IM聊天服务器


    IDE visual studio 2019
    运行系统Centos 7
    cmake工程
    include 目录是头文件
    source 目录是源文件

    1. CentOS7安装cmake

    cd /opt/
    wget https://github.com/Kitware/CMake/releases/download/v3.24.1/cmake-3.24.1.tar.gz
    tar -zaxf cmake-3.24.1.tar.gz
    cd cmake-3.24.1 && ./configure 
    make && make install 
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2. vs 2019添加跨平台主机

    1. 工具 --> 选择 -->跨平台–>添加主机
      在这里插入图片描述
      在这里插入图片描述

    3. 创建cmake工程项目

    1. 创建项目
      在这里插入图片描述
      在这里插入图片描述

    2. 修改远程主机
      在这里插入图片描述
      在这里插入图片描述
      在这里插入图片描述
      在这里插入图片描述

    3. 新建目录include 和 source
      在这里插入图片描述



    4. 修改CMakeList.txt文件

    # CMakeList.txt: chatserver 的 CMake 项目,在此处包括源代码并定义
    # 项目特定的逻辑。
    #
    cmake_minimum_required (VERSION 3.8)
    
    #工程名
    project ("chatserver")
    
    
    # 添加include路径,也就是头文件路径
    #添加source路径,也就是添加源文件
    #搜索的目录
    include_directories("./include" "./source") 
    aux_source_directory("./include" INCLUDE_LIST)
    aux_source_directory("./source" SRC_LIST)
    
    #创建需要的目录
    file(MAKE_DIRECTORY "logs" "conf")
    
    
    
    # TODO: 如有需要,请添加测试并安装目标。
    #指定生成文件输出路径
    SET(EXECUTABLE_OUTPUT_PATH ${
       PROJECT_SOURCE_DIR}/sbin)
    
    # 将源代码添加到此项目的可执行文件。
    #add_executable (chatserver   "main.cpp" ${
         SRC_LIST} ${
         INCLUDE_LIST})
    add_executable (chatserver  ${
       SRC_LIST} ${
       INCLUDE_LIST})
    
    
    # 指定编译器
    # CMAKE_C_FLAGS_DEBUG          ----  C 编译器
    # CMAKE_CXX_FLAGS_DEBUG        ----  C++ 编译器
    # -std=c++11  使用 C++11
    # -g:只是编译器,在编译的时候,产生调试信息。
    # -Wall:生成所有警告信息。一下是具体的选项,可以单独使用
    # -L/usr/lib64/mysql -lmysqlclient -lpthread -lm -lrt -ldl"  数据库使用
    # 如果想要生成的可执行文件拥有符号表,可以gdb调试,就直接加上这句
    add_definitions("-Wall -g")
    
    # 如果代码需要支持C++11,就直接加上这句
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -L/usr/lib64/mysql -lmysqlclient -lpthread -lm -lrt -ldl")
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    5. 添加程序启动配置文件

    1. 头文件config.h 添加到include目录
    #ifndef __CONFIG_H_
    #define __CONFIG_H_
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    //定义配置文件注释符号
    constexpr auto COMMENT_CHAR = '#';
    
    
    using namespace std;
    class Config_handle {
       
    public:
    	Config_handle();
    	~Config_handle();
    
    private:
    
    	//读取配置文件文件
    	bool read_config_file(const string& filename, map<string, string>& m);
    
    	//逐行读取处理
    	bool analyse_line(const string& line, string& key, string& value);
    
    	//去空格
    	void trim_str(string& str);
    
    	//是否空格或者tab键
    	bool is_space_or_tab(char c);
    
    
    public:
    	void config_init();
        //获取配置文件键值对
    	map<string, string> get_config_keys();
    
    private:
    	map<string, string>maps;
    
    	//配置文件是否存在
    	bool file_exist;
    
    	//配置文件路径
    	const string config_file_path = "../conf/server.conf";
    };
    #endif //! __CONFIG_H_
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    1. 添加源文件 config.cpp 到source里面
    #include "config.h"
    #include "server.h"
    
    Config_handle::~Config_handle()
    {
       
    }
    
    Config_handle::Config_handle()
    {
       
    	file_exist = read_config_file(config_file_path, maps);
    }
    
    void Config_handle::config_init() {
       
    
    	//读取配置文件内容
    	if (file_exist) {
       
    		map<string, string>::iterator iter;
    		//	for (itr = maps.begin(); itr != maps.end(); ++itr) {
       
    		//		cout << itr->first <<"="<< itr->second << endl;;
    		//	}
    
    		//socket ip
    		string server_ip;
    		iter = maps.find("server_ip");
    		if (iter != maps.end()) {
       
    			server_ip = iter->second;
    			//判断ip是否为合法ipv4
    			if (inet_addr(server_ip.c_str()) == -1) {
       
    				cout << "\e[0;31mserver_host error:\e[0m ipv4 syntax error" << endl;
    				exit(-1);
    			}
    		}
    
    		//socket 端口
    		string server_port;
    		iter = maps.find("server_port");
    		if (iter != maps.end()) {
       
    			server_port = iter->second;
    			//判断端口是否为数字
    			for (int i = 0; i < server_port.length(); i++) {
       
    				if (!(isdigit(server_port[i]))) {
       
    					cout << "\e[0;31mserver_port error:\e[0m port not support!" << endl;
    					exit(-1);
    				}
    			}
    			//判断端口是否为合法端口范围
    			if (!(0 < (atoi(server_port.c_str())) && (atoi(server_port.c_str())) < 65535)) {
       
    				cout << "\e[0;31mserver port error:\e[0m value range 0-65535 " << endl;;
    				exit(-1);
    			}
    		}
    		else {
       
    			cout << "\e[0;31mserver_host error:\e[0m missing server_host error" << endl;
    			exit(-1);
    		}
    
    		//DB数据库配置
    		//db_host 数据库主机
    		string db_host;
    		iter = maps.find("db_host");
    		if (iter != maps.end()) {
       
    			db_host = iter->second;
    			size_t point_position = db_host.find('.');
    			//判断是域名还是ipv4
    			if (point_position != string::npos) {
       
    				int s = 1;
    				for (int i = 0; i < point_position; i++) {
       
    					if (!(isdigit(db_host[i]))) {
       
    						s = 0;
    					}
    				}
    				//ipv4形式
    				if (s) {
       
    					//判断ip是否为合法ipv4
    					if (inet_addr(db_host.c_str()) == -1) {
       
    						cout << "\e[0;31mdb_host error:\e[0m ipv4 syntax error" << endl;
    						exit(-1);
    					}
    				}
    			}
    		}
    		else {
       
    			cout << "\e[0;31mdb_host error:\e[0m missing db_host error" << endl;
    			exit(-1);
    		}
    
    		//db_port 数据库端口
    		string db_port;
    		iter = maps.find("db_port");
    		if (iter != maps.end()) {
       
    			db_port = iter->second;
    			//判断端口是否为数字
    			for (int i = 0; i < db_port.length(); i++) {
       
    				if (!(isdigit(db_port[i]))) {
       
    					cout << "\e[0;31mdb_port error:\e[0m port not support!" << endl;
    					exit(-1);
    				}
    			}
    			//判断端口是否为合法端口范围
    			if (!(0 < (atoi(db_port.c_str())) && (atoi(db_port.c_str())) < 65535)) {
       <
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
  • 相关阅读:
    【nodejs案例】记账本功能 -- 如何配置API(三)
    CSS中的浮动float(元素怎样浮动以及浮动元素的特点--脱标)
    Swagger的简单介绍,集成,以及如何在生产环境中关闭swagger,在测试和开发环境中自动打开
    java计算机毕业设计高校车辆管理系统MyBatis+系统+LW文档+源码+调试部署
    一文揭开,测试外包公司的真 相
    忆联分布式数据库存储解决方案,助力MySQL实现高性能、低时延
    STM32CubeIDE+STLINK调试和下载代码
    百度文心一言4.0抢先体验教程!
    Rancher部署K8S集群
    KMP算法
  • 原文地址:https://blog.csdn.net/weixin_41560737/article/details/126698578