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

Muduo库编译学习(1)

1.muduo库简介

muduo是由Google大佬陈硕开发,是一个基于非阻塞IO和事件驱动的现代C++网络库,原生支持one loop per thread这种IO模型,该库只支持Linux系统,网上大佬对其褒贬不一,作为小白用来学习就无可厚非了。
git仓库:https://github.com/chenshuo/muduo

2.准备事项

  1. muduo采用Cmake为build system,安装如下:
$ sudo apt-get install cmake
  1. muduo依赖于Boost,安装如下:
$ sudo apt-get install libboost-dev libboost-test-dev
  1. curl、c-ares DNS、Google Protobuf这3个库非必须安装,如果安装了cmake会多编译一些示例,安装如下:
$ sudo apt-get install libcur14-openssl-dev libc-ares-dev
$ sudo apt-get install protobuf-compiler libprotobuf-dev

3.编译

  1. 下载muduo-master解压后,执行编译脚本
// 切换路径
$ cd muduo-master/
//编译muduo库,默认release版,生成build文件夹
$ ./build.sh -j2
// 将muduo头文件和库文件安装到release-install目录下的include和lib文件夹
$ ./build.sh install
// 将头文件拷贝到系统路径
$ cd build/release-install-cpp11/include/
$ cp -r muduo/ /usr/local/include/
// 将库文件拷贝到系统路径
$ cd build/release-install-cpp11/lib/
$ cp * /usr/local/lib/

4.测试demo

  1. EchoServer.h
#include <muduo/net/TcpServer.h>
#include <muduo/base/Logging.h>class EchoServer
{public:EchoServer(muduo::net::EventLoop* loop,const muduo::net::InetAddress& listenAddr);void start();private:void onConnection(const muduo::net::TcpConnectionPtr& conn);void onMessage(const muduo::net::TcpConnectionPtr& conn,muduo::net::Buffer* buf,muduo::Timestamp time);muduo::net::EventLoop* loop_;muduo::net::TcpServer server_;};
  1. EchoServer.cpp
#include "EchoServer.h"
#include <boost/bind/bind.hpp>using namespace boost::placeholders;EchoServer::EchoServer(muduo::net::EventLoop* loop,const muduo::net::InetAddress& listenAddr):loop_(loop),server_(loop, listenAddr, "EchoServer"){server_.setConnectionCallback(boost::bind(&EchoServer::onConnection, this, _1));server_.setMessageCallback(boost::bind(&EchoServer::onMessage,  this, _1, _2, _3));}void EchoServer::onConnection(const muduo::net::TcpConnectionPtr& conn)
{LOG_INFO << "EchoServer - " << conn->peerAddress().toIpPort() << " -> "<< conn->localAddress().toIpPort() << " is "<< (conn->connected()? "UP" : "DOWN");
}void EchoServer::onMessage(const muduo::net::TcpConnectionPtr& conn,muduo::net::Buffer* buf,muduo::Timestamp time)
{muduo::string msg(buf->retrieveAllAsString());LOG_INFO << conn->name() << "echo - " << msg.size() << " bytes, "<< " data received at " << time.toString();conn->send(msg);
}void EchoServer::start()
{server_.start();
}
  1. test.cpp
#include "EchoServer.h"#include <muduo/net/EventLoop.h>
#include <muduo/base/Logging.h>using namespace muduo;
using namespace muduo::net;int main()
{LOG_INFO << "pid = "<< getpid();muduo::net::EventLoop loop;muduo::net::InetAddress listenAddr(2007);EchoServer server(&loop,listenAddr);server.start();loop.loop();
}
  1. 在编译的时候出现好多未定义的错误,最后排查是因为编译的时候链接库的顺序有要求,编译如下:
$ g++ EchoServer.cpp test.cpp -o test -lmuduo_base -lmuduo_net -lpthread

错误消息如下:
在这里插入图片描述

5.测试

执行test.out程序启动服务端,再通过终端模拟客户端建立连接,在客户端发送消息会同时接收服务端回复的相同消息。
在这里插入图片描述

http://www.lryc.cn/news/310013.html

相关文章:

  • 【研发日记】Matlab/Simulink技能解锁(三)——在Stateflow编辑窗口Debug
  • ZYNQ--MIG核配置
  • MySQL 多表查询 连接查询 内连接
  • T - SQL使用事务 及 在Winform使用事务
  • string(三)--实现
  • textbox跨线程写入
  • 幻兽帕鲁专用服务器搭建之Linux部署配置教程
  • Kubernetes: 本地部署dashboard
  • Outlook邮箱IMAP密码怎么填写?账户设置?
  • [计算机网络]--I/O多路转接之poll和epoll
  • 【NTN 卫星通信】卫星和无人机配合的应用场景
  • Git 分布式版本控制系统
  • ng : 无法将ng项识别为 cmdlet、函数、脚本文件或可运行程序的名称
  • iOS小技能:苹果书签打包教程【WebClip描述文件(WebClip Configuration Profile)】
  • Spring Cloud项目合规性注册之-(单元集成测试报告)
  • IntelliJ IDEA 常用的插件
  • 超详细红黑树的模拟实现
  • 【亚马逊云科技】通过Amazon CloudFront(CDN)快速访问资源
  • ES-ES的基本概念
  • 排序算法——快速排序的非递归写法
  • 【论文阅读】基于人工智能目标检测与跟踪技术的过冷流沸腾气泡特征提取
  • RabbitMQ讲解与整合
  • python 基础知识点(蓝桥杯python科目个人复习计划56)
  • 【vue】vue中数据双向绑定原理/响应式原理,mvvm,mvc、mvp分别是什么
  • 基于反光柱特征的激光定位算法思路
  • CSM是什么意思?
  • ES6 面试题
  • 智能指针(C++)
  • 社区店商业模式探讨:如何创新并持续盈利?
  • 一些可以访问gpt的方式