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
2. vs 2019添加跨平台主机
- 工具 --> 选择 -->跨平台–>添加主机
3. 创建cmake工程项目
-
创建项目
-
修改远程主机
-
新建目录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")
5. 添加程序启动配置文件
- 头文件config.h 添加到include目录
#ifndef __CONFIG_H_
#define __CONFIG_H_
#include <map>
#include <iostream>
#include <string>
#include <fstream>
#include <arpa/inet.h>
#include <algorithm>
//定义配置文件注释符号
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_
- 添加源文件 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)) {
cout << "\e[0;31mdb port error:\e[0m value range 0-65535 " << endl;;
exit(-1);