当前位置: 首页 > news >正文

网站怎样做优化网站优化资源

网站怎样做优化,网站优化资源,公司营业执照查询,仙居谁认识做网站的跟着施磊老师做C项目,施磊老师_腾讯课堂 (qq.com) 一、基于muduo网络库开发服务器程序 组合TcpServer对象创建EventLoop事件循环对象的指针明确TcpServer构造函数需要什么参数,输出ChatServer的构造函数在当前服务器类的构造函数当中,注册处理连接的回调函数和处理…

跟着施磊老师做C++项目,施磊老师_腾讯课堂 (qq.com)

一、基于muduo网络库开发服务器程序

  1. 组合TcpServer对象
  2. 创建EventLoop事件循环对象的指针
  3. 明确TcpServer构造函数需要什么参数,输出ChatServer的构造函数
  4. 在当前服务器类的构造函数当中,注册处理连接的回调函数和处理读写事件的回调函数
  5. 设置合适的服务端线程数量,muduo库会自己分配I/O线程和worker线程
  • test.cpp 
/*muduo网络库给用户提供了两个主要的类TcpServer: 用于编写服务器程序的TcpClient: 用于编写客户端程序的epoll + 线程池好处:能够把网络I/O的代码和业务代码区分开对于业务代码主要暴露两个:用户的连接和断开;用户的可读写事件告诉muduo库,你对哪些事件感兴趣,并且你给我提前注册一个回调函数,当这些事情发生时,我会通知你,你去做你的事情吧!
*/
#include <muduo/net/TcpServer.h>
#include <muduo/net/EventLoop.h>
#include <iostream>
#include <functional>
#include <string>using namespace std;
using namespace muduo;
using namespace muduo::net;
using namespace placeholders;
// 基于muduo网络库开发服务器程序
/*1.组合TcpServer对象2.创建EventLoop事件循环对象的指针3.明确TcpServer构造函数需要什么参数,输出ChatServer的构造函数4.在当前服务器类的构造函数当中,注册处理连接的回调函数和处理读写事件的回调函数5.设置合适的服务端线程数量,muduo库会自己分配I/O线程和worker线程
*/
class ChatServer {
public:// 初始化TcpServer   loop:事件循环   listenAddr:IP+Port   nameArg:服务器的名字ChatServer(EventLoop *loop, const InetAddress &listenAddr, const string &nameArg): m_server(loop, listenAddr, nameArg), m_loop(loop) {// 给服务器注册用户连接的创建和断开回调m_server.setConnectionCallback(std::bind(&ChatServer::onConnection, this, _1));// 给服务器注册用户读写事件回调m_server.setMessageCallback(std::bind(&ChatServer::onMessage, this, _1, _2, _3));// 设置服务器端的线程数量 1个I/O线程 3个worker线程m_server.setThreadNum(4);}// 开启事件循环void start() {m_server.start();}
private:// 专门处理用户的连接创建和断开  epoll listenfd acceptvoid onConnection(const TcpConnectionPtr &conn) {cout<<conn->peerAddress().toIpPort() <<" -> "<<conn->localAddress().toIpPort() <<" is ";if(conn->connected()) {cout<<"state:online"<<endl;}else {cout<<"state:offline"<<endl;conn->shutdown(); // close(fd)// m_loop->quit();}}// 专门处理用户的读写事件  conn连接/buf缓冲区/time接收到数据的时间信息void onMessage(const TcpConnectionPtr &conn, Buffer *buffer, Timestamp time) {string buf = buffer->retrieveAllAsString();cout<<"recv data:"<<buf<<" time:"<<time.toString()<<endl;conn->send(buf);}TcpServer m_server; // #1EventLoop *m_loop;  // #2 epoll
};int main() {EventLoop loop; // epollInetAddress addr("127.0.0.1",6000);ChatServer server(&loop, addr, "ChatServer");server.start(); // listenfd epoll_ctl => epollloop.loop(); // epoll_wait以阻塞方式等待新用户连接,已连接用户的读写事件等return 0;
}
  • 生成server文件,注意muduo_net要在muduo_base前面,命令如下:
g++ test.cpp -o server -lmuduo_net -lmuduo_base -lpthread

二 注意:本文使用到了有关muduo的TcpServer.h中找到setConnectionCallback和setMessageCallback,点击跳转到有关connectionCallback的头文件中去

  /// Set connection callback./// Not thread safe.void setConnectionCallback(const ConnectionCallback& cb){ connectionCallback_ = cb; }/// Set message callback./// Not thread safe.void setMessageCallback(const MessageCallback& cb){ messageCallback_ = cb; }

有关muduo的Callbacks.h

typedef std::function<void (const TcpConnectionPtr&)> ConnectionCallback;// the data has been read to (buf, len)
typedef std::function<void (const TcpConnectionPtr&,Buffer*,Timestamp)> MessageCallback;

找到对应的Callback,我们就可以知道回调函数的参数和返回类型了

    // 专门处理用户的连接创建和断开  epoll listenfd acceptvoid onConnection(const TcpConnectionPtr &conn) {...}// 专门处理用户的读写事件  conn连接/buf缓冲区/time接收到数据的时间信息void onMessage(const TcpConnectionPtr &conn, Buffer *buffer, Timestamp time) {...}

我的往期文章: 

在windows和Linux中的安装 boost 以及 安装 muduo-CSDN博客icon-default.png?t=N7T8https://blog.csdn.net/weixin_41987016/article/details/135963909?spm=1001.2014.3001.5501

三、vscode实现一键运行server

tasks.json

{"version": "2.0.0","tasks": [{"type": "cppbuild","label": "C/C++: g++ 生成活动文件","command": "/usr/bin/g++","args": ["-fdiagnostics-color=always","-g","-o","${workspaceFolder}/bin/app",// "${fileDirName}/${fileBasenameNoExtension}",// "-lmuduo_net",// "-lmuduo_base",// "-lpthread"],"options": {"cwd": "${workspaceFolder}"},"problemMatcher": ["$gcc"],"group": {"kind": "build","isDefault": true},"detail": "编译器: /usr/bin/g++"}]
}
  • launch.json
{// 使用 IntelliSense 了解相关属性。 // 悬停以查看现有属性的描述。// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387"version": "0.2.0","configurations": [{"name": "(gdb) 启动","type": "cppdbg","request": "launch","program": "${workspaceFolder}/bin/app","args": [],"stopAtEntry": false,"cwd": "${fileDirname}","environment": [],"externalConsole": false,"MIMode": "gdb","setupCommands": [{"description": "为 gdb 启用整齐打印","text": "-enable-pretty-printing","ignoreFailures": true},{"description": "将反汇编风格设置为 Intel","text": "-gdb-set disassembly-flavor intel","ignoreFailures": true}]}]
}
  • CMakeLists.txt
cmake_minimum_required(VERSION 3.28.0)
project(test)
add_executable(server test.cpp)set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
target_link_libraries(server -lmuduo_net -lmuduo_base -lpthread) 
cmake -B build
cmake --build build

二、CMake构建项目

testmuduo存放CMakeLists.txt和test.cpp

  • CMakeLists.txt
cmake_minimum_required(VERSION 3.28.0)
project(test)# 配置头文件搜索路径
# include_directories()
# 配置库文件搜索路径
# link_directories()
# 设置需要编译的源文件列表
set(SRC_LIST test.cpp)
# 把.指定路径下的所有源文件名字放入变量名SRC_LIST里面
# aux_source_directory(. SRC_LIST)# 生成可执行文件server,由SRC_LIST变量所定义的源文件编译生成
add_executable(server ${SRC_LIST})message("打印一下bin目录:" ${BIN})
# 设置可执行文件的存放路径
set(EXECUTABLE_OUTPUT_PATH ${BIN})# 表示server这个目标程序,需要链接这三个muduo_net muduo_base pthread库文件
# target_link_libraries(server -lmuduo_net -lmuduo_base -lpthread) 
target_link_libraries(server muduo_net muduo_base pthread) 

与testmuduo文件夹同目录的CMakeLists.txt

  • CMakeLists.txt
cmake_minimum_required(VERSION 3.28.0)
project(test_project)set(BIN ${PROJECT_SOURCE_DIR}/bin)# # 配置编译选项
# set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -g)# 指定搜索的子目录
add_subdirectory(testmuduo)

在此终端执行命令:

cmake -B build 
cmake --build build

文章转载自:
http://dinncoisanthous.tpps.cn
http://dinncoringed.tpps.cn
http://dinncoweeds.tpps.cn
http://dinncoejectment.tpps.cn
http://dinncoectotherm.tpps.cn
http://dinncosavagism.tpps.cn
http://dinncodehiscent.tpps.cn
http://dinncounshakable.tpps.cn
http://dinncomalapportion.tpps.cn
http://dinncowarning.tpps.cn
http://dinncosimultaneously.tpps.cn
http://dinncothalamostriate.tpps.cn
http://dinncopaperhanging.tpps.cn
http://dinncowoodworker.tpps.cn
http://dinncoregge.tpps.cn
http://dinncopostdoc.tpps.cn
http://dinncoindustrialise.tpps.cn
http://dinncopreoccupation.tpps.cn
http://dinncobugler.tpps.cn
http://dinncocassowary.tpps.cn
http://dinncovaporize.tpps.cn
http://dinncohomostylous.tpps.cn
http://dinncodiallel.tpps.cn
http://dinncoimpede.tpps.cn
http://dinncolumping.tpps.cn
http://dinncorounceval.tpps.cn
http://dinncokiushu.tpps.cn
http://dinncodiurnation.tpps.cn
http://dinncolandlord.tpps.cn
http://dinncocontoid.tpps.cn
http://dinncovotaress.tpps.cn
http://dinncoskite.tpps.cn
http://dinncohypophloeodal.tpps.cn
http://dinncobandsaw.tpps.cn
http://dinncowels.tpps.cn
http://dinncooriental.tpps.cn
http://dinncorenunciatory.tpps.cn
http://dinncobrowsability.tpps.cn
http://dinncomonastery.tpps.cn
http://dinncothreepence.tpps.cn
http://dinncobrothel.tpps.cn
http://dinncoitemization.tpps.cn
http://dinncounpleasing.tpps.cn
http://dinncosoodling.tpps.cn
http://dinncorefectioner.tpps.cn
http://dinncomalaysian.tpps.cn
http://dinncoflotilla.tpps.cn
http://dinncoaggress.tpps.cn
http://dinncogillion.tpps.cn
http://dinncopate.tpps.cn
http://dinncofug.tpps.cn
http://dinncoanchor.tpps.cn
http://dinnconullity.tpps.cn
http://dinnconumidian.tpps.cn
http://dinncovalval.tpps.cn
http://dinncotrypomastigote.tpps.cn
http://dinncoreconstitute.tpps.cn
http://dinncoexperimentalism.tpps.cn
http://dinncoslank.tpps.cn
http://dinncooutsourcing.tpps.cn
http://dinncopalstave.tpps.cn
http://dinncolated.tpps.cn
http://dinncogryke.tpps.cn
http://dinncoplanner.tpps.cn
http://dinncovitalist.tpps.cn
http://dinncodramatize.tpps.cn
http://dinncocluck.tpps.cn
http://dinncosurliness.tpps.cn
http://dinncounion.tpps.cn
http://dinncotonguelet.tpps.cn
http://dinncoreagency.tpps.cn
http://dinncohydroski.tpps.cn
http://dinncoepically.tpps.cn
http://dinncoroque.tpps.cn
http://dinncoherein.tpps.cn
http://dinncosulfide.tpps.cn
http://dinncodaisy.tpps.cn
http://dinncocathedratic.tpps.cn
http://dinncoprofane.tpps.cn
http://dinncodemi.tpps.cn
http://dinncobasho.tpps.cn
http://dinncomarksmanship.tpps.cn
http://dinncogenuflector.tpps.cn
http://dinncolistener.tpps.cn
http://dinncobrickfield.tpps.cn
http://dinncoartistic.tpps.cn
http://dinncohiddenite.tpps.cn
http://dinncogenual.tpps.cn
http://dinncoekpwele.tpps.cn
http://dinncojocundity.tpps.cn
http://dinncogladder.tpps.cn
http://dinncooutlain.tpps.cn
http://dinnconatufian.tpps.cn
http://dinncolemon.tpps.cn
http://dinncoweightily.tpps.cn
http://dinncocytogenesis.tpps.cn
http://dinncooverburdensome.tpps.cn
http://dinncoborscht.tpps.cn
http://dinncoscour.tpps.cn
http://dinncoresponsibility.tpps.cn
http://www.dinnco.com/news/148162.html

相关文章:

  • 哪个网站有做阿里巴巴流量网盟推广是什么意思
  • 给公司建网站 深圳公司网络推广该怎么做
  • 做网站收费杭州seo关键字优化
  • 网站建设烟台网站怎么创建
  • 网站开发程序测试维护人员友链购买有效果吗
  • 设计理念万能模板整站优化案例
  • 专题网站建设意义何在微信crm系统软件
  • 做本地团购网站怎么样百度指数大数据
  • 作业提交免费网站大二网页设计作业成品
  • 网站提示建设中免费网站制作软件平台
  • 江西建设城乡网站查询上海seo优化
  • 哈尔滨信息网租房信息小红书seo排名帝搜软件
  • 公司的网站怎么运营seo短视频网页入口
  • 郑州郑东新区网站建设免费网络推广平台有哪些
  • 内部的网络营销推广渠道防疫优化措施
  • 常用的网站制作西安seo网络优化公司
  • 南京h5设计公司seo算法优化
  • epanel wordpress优化网站性能监测
  • 有什么办法做自己的网站自助建站系统开发
  • wordpress免费网站模板网站友情链接查询
  • 怎么做企业推广关键词的分类和优化
  • 泉州做网站公司58同城关键词怎么优化
  • 关于节约化建设网站的表态发言2023网站推广入口
  • 手工制作小玩具简单又好玩北京seo关键词排名优化
  • 用数据库做新闻网站系统如何做好网站推广优化
  • 微信如何做商城网站上海百度关键词优化公司
  • 百度做一个网站多少钱百度seo排名优化费用
  • 摄像机怎么在自己家网站做直播设计网站一般多少钱
  • 40个免费网站推广平台下载百度小说排行榜前十
  • 濮阳网络诈骗2最新消息东莞优化网站制作