栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

Linux C++ 通信 - 服务器架构初步

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Linux C++ 通信 - 服务器架构初步

Linux C++ 通信 - 服务器架构初步

明镜止水,长话短说。
终于到这步了,哈→哈→哈⬆哈⬇


目录
  • Linux C++ 通信 - 服务器架构初步
    • 目录规划
    • C++源代码编写
    • makefile && cmake

目录规划

目录将是整个服务器开发的结构,先按最简单的来做,目录先只分一级,为方便使用cmake工具生成makefile文件,在根目录以及signal目录增加CMakeLists.txt文件,具体如下:

root@ubuntu:/mnt/hgfs/c/CatServer# apt  install tree
root@ubuntu:/mnt/hgfs/c/CatServer# tree
.
├── app                 //存放应用程序核心文件
│   ├── cat_conf.cpp    //辅助功能
│   └── cat.cpp         //服务器入口
├── CMakeLists.txt      //cmake起点
├── _include            //存放头文件,函数声明等
│   ├── cat_func.h      
│   ├── cat_signal2.h
│   └── cat_signal.h
├── misc                //存放不好分类的文件
├── net                 //存放和网络处理相关的文件
├── proc                //存放和线程处理相关的文件
└── signal              //存放和信号处理相关的文件
    ├── cat_signal.cpp
    └── CMakeLists.txt  

6 directories, 8 files

root@ubuntu:/mnt/hgfs/c/CatServer# 

整个服务器名就叫cat吧(猫,doge),懒得取名字了。
建议备份一下整个服务器文件夹。

C++源代码编写

先随便写,等能通过总编译再来修改
首先是app/cat_conf.cpp 和 app/cat.cpp

//  app/cat.cpp
#include
#include
#include
#include

#include "cat_func.h"
#include "cat_signal.h"

using namespace std;

int main(int argc, char *const *argv){

    printf("主程序开始运行!n");

    sleep(1);
    cat_conf();

    sleep(1);
    signal_demo();

    printf("程序退出,再见!n");
    return 0;
}
// app/cat_conf.cpp
#include
#include
#include
#include
using namespace std;

void cat_conf(){
    printf("辅助功能函数开始运行!n");
    return ;
}

然后是signal/cat_signal.cpp

// signal/cat_signal.cpp
#include
#include
#include
#include
using namespace std;

void signal_demo(){
    printf("信号函数开始运行!n");
    return ;
}

最后是头文件编写:

// _include/cat_func.h
# ifndef __CAT_CONF_H__
# define __CAT_CONF_H__

void cat_conf();

# define VERSION "1.0"
# endif
// _include/cat_signal.h
# ifndef __CAT_SIGNAL_H__
# define __CAT_SIGNAL_H__

# include "cat_signal2.h"
void signal_demo();

# endif
// _include/cat_signal2.h
# ifndef __CAT_SIGNAL2_H__
# define __CAT_SIGNAL2_H__

void signal_demo2();

# endif
makefile && cmake

感觉这个问题避不开,还是认真写写吧。
makefile语法看看其它大佬写的:makefile
cmake使用:cmake

根目录CMakeLists.txt文件编写:

cmake_minimum_required(VERSION 3.18)

project(CatServer)

add_subdirectory(signal)

include_directories(${PROJECT_SOURCE_DIR}/_include)

add_definitions("-g -Wall -std=c++11")

add_executable(catServer
	${PROJECT_SOURCE_DIR}/app/cat.cpp
	${PROJECT_SOURCE_DIR}/app/cat_conf.cpp)

target_link_libraries(catServer SIGNAL_DIR)

使用cmake版本3.18,项目名CatServer,加入文件夹signal,加载头文件夹_include,增加编译参数-g -Wall -std=c++11,编译成可执行文件catServer,增加库SIGNAL_DIR。

signal文件夹下CMakeLists.txt文件编写:

aux_source_directory(. SIGNAL_LIB_SRCS)
add_library(SIGNAL_DIR ${SIGNAL_LIB_SRCS})

将当前文件夹所有源代码链接成SIGNAL_DIR库

使用cmake和make:

root@ubuntu:/mnt/hgfs/c/CatServer# cmake CMakeLists.txt 
-- The C compiler identification is GNU 10.3.0
-- The CXX compiler identification is GNU 10.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /mnt/hgfs/c/CatServer
root@ubuntu:/mnt/hgfs/c/CatServer# make
Scanning dependencies of target SIGNAL_DIR
[ 20%] Building CXX object signal/CMakeFiles/SIGNAL_DIR.dir/cat_signal.cpp.o
[ 40%] Linking CXX static library libSIGNAL_DIR.a
[ 40%] Built target SIGNAL_DIR
Scanning dependencies of target catServer
[ 60%] Building CXX object CMakeFiles/catServer.dir/app/cat.cpp.o
[ 80%] Building CXX object CMakeFiles/catServer.dir/app/cat_conf.cpp.o
[100%] Linking CXX executable catServer
[100%] Built target catServer
root@ubuntu:/mnt/hgfs/c/CatServer# 

成功编译完成后,会生成一个可执行catServer和一些编译日志

root@ubuntu:/mnt/hgfs/c/CatServer# ll
total 145
drwxrwxrwx 1 root root  4096 Aug  5 03:38 ./
drwxrwxrwx 1 root root 49152 Aug  4 05:11 ../
drwxrwxrwx 1 root root  4096 Aug  5 00:15 app/
-rwxrwxrwx 1 root root 59632 Aug  5 03:38 catServer*   #可执行文件
-rwxrwxrwx 1 root root 13674 Aug  5 03:33 CMakeCache.txt*
drwxrwxrwx 1 root root  4096 Aug  5 03:38 CMakeFiles/
-rwxrwxrwx 1 root root  1769 Aug  5 03:33 cmake_install.cmake*
-rwxrwxrwx 1 root root   336 Aug  5 01:02 CMakeLists.txt*
drwxrwxrwx 1 root root     0 Aug  4 05:11 _include/
-rwxrwxrwx 1 root root  6502 Aug  5 03:38 Makefile*
drwxrwxrwx 1 root root     0 Aug  4 04:55 misc/
drwxrwxrwx 1 root root     0 Aug  4 04:55 net/
drwxrwxrwx 1 root root     0 Aug  4 04:55 proc/
drwxrwxrwx 1 root root  4096 Aug  5 03:38 signal/

运行可执行文件:

root@ubuntu:/mnt/hgfs/c/CatServer# ./catServer 
主程序开始运行!
辅助功能函数开始运行!
信号函数开始运行!
程序退出,再见!
root@ubuntu:/mnt/hgfs/c/CatServer# 

明天开始服务器日志开发!
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1033252.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号