1. Linux C++ muduo 库学习——库的编译安装

1. 下载

bash 复制代码
https://github.com/chenshuo/muduo

2. 安装依赖库

1. 安装 boost 库

bash 复制代码
sudo apt-get update
sudo apt-get install libboost-all-dev

下面我们测试一下 boost 库

cpp 复制代码
#define BOOST_BIND_GLOBAL_PLACEHOLDERS

#include <iostream>
#include <boost/bind.hpp>
#include <string>
using namespace std;

class Hello
{
public:
	void say(string name) 
	{ cout << name << " talk to America.!" << endl; }
};

int main()
{
	Hello h;
	auto func = boost::bind(&Hello::say, &h, "Li si");
	func();
	return 0;
}

代码写好了,开始编译一下:

bash 复制代码
g++ test.cpp -o test -std=c++11

2. 安装 cmake

bash 复制代码
sudo apt install cmake  -y

3. 编译

bash 复制代码
./build.sh

4. 手动安装头文件和 .a (默认静态编译)

1. 头文件

bash 复制代码
sudo cp muduo/ /usr/include/

2. 库文件

进入到 build 目录下面的 lib 目录,将所有文件拷贝到

bash 复制代码
sudo cp * /usr/local/lib/

5. 测试 muduo 库

创建一个 C++ 文件

cpp 复制代码
#include <muduo/net/TcpServer.h>
#include <muduo/base/Logging.h>
#include <boost/bind.hpp>
#include <muduo/net/EventLoop.h>

// 使用muduo开发回显服务器
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::TcpServer server_;
};

EchoServer::EchoServer(muduo::net::EventLoop* loop,
                       const muduo::net::InetAddress& listenAddr)
  : server_(loop, listenAddr, "EchoServer")
{
  server_.setConnectionCallback(
      boost::bind(&EchoServer::onConnection, this, _1));
  server_.setMessageCallback(
      boost::bind(&EchoServer::onMessage, this, _1, _2, _3));
}

void EchoServer::start()
{
  server_.start();
}

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);
}


int main()
{
  LOG_INFO << "pid = " << getpid();
  muduo::net::EventLoop loop;
  muduo::net::InetAddress listenAddr(8888);
  EchoServer server(&loop, listenAddr);
  server.start();
  loop.loop();
}

代码编译:

bash 复制代码
g++ test_muduo1.cc -o test_muduo1 -lmuduo_net -lmuduo_base -lpthread -std=c++11

测试:

终端1:

bash 复制代码
./test_muduo1 

终端2:

bash 复制代码
echo "hello world" | nc localhost 8888

测试结果如下:

相关推荐
m0_683124791 分钟前
Ubuntu服务设置开机自启
linux·运维·ubuntu
BestOrNothing_20154 分钟前
(1)双系统中Ubuntu22.04启动盘制作与启动盘恢复全过程
linux·ubuntu·双系统·启动盘制作·启动盘恢复
AI成长日志9 分钟前
【实用工具教程】Linux常用命令速查与实战场景:文件操作、进程管理与网络调试高频命令解析
linux·php
落叶花开又一年13 分钟前
检验检测机构资质认定远程评审工作程序
linux·运维·服务器
CDA数据分析师干货分享15 分钟前
汉江师范学院数据科学与大数据技术专业大二学生:CDA一级学习经验
大数据·经验分享·学习·数据分析·cda证书·cda数据分析师
SuniaWang18 分钟前
《Spring AI + 大模型全栈实战》学习手册系列 · 专题四:《Ollama 模型管理与调优:让 AI 模型在低配服务器上流畅运行》
人工智能·学习·spring
idolao20 分钟前
CentOS 7 安装 jdk-7u25-linux-x64.tar.gz 详细步骤(解压配置环境变量)
linux
tankeven22 分钟前
HJ137 乘之
c++·算法
冰水不凉35 分钟前
cartographer源码阅读四-MapBuilder
学习·slam
add45a1 小时前
C++中的观察者模式
开发语言·c++·算法