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

测试结果如下:

相关推荐
.柒宇.14 分钟前
AI掘金头条项目部署实践指南
linux·运维·python·fastapi
小郑加油27 分钟前
python学习Day11:认识与创建CSV文件
开发语言·python·学习
AI360labs_atyun42 分钟前
清华开源AI导师OpenMAIC!30秒生成互动课堂!还能学“养龙虾”
人工智能·科技·学习·ai
学机械的鱼鱼1 小时前
【学习笔记】QGroundControl安装与使用简明指南
笔记·学习
zhangrelay1 小时前
Ubuntu 18.04 经典 / 有趣 / 实用 APT 软件清单
linux·笔记·学习·ubuntu
FserSuN1 小时前
Machine Learning Specialization - Week 1, 1-8学习总结
人工智能·学习·机器学习
晚风吹红霞1 小时前
C++异常处理核心知识点全解析
开发语言·c++
CoderCodingNo1 小时前
【信奥业余科普】C++ 的奇妙之旅 | 17:面的铺展与文本的本质——二维数组与字符串
开发语言·c++
不做无法实现的梦~1 小时前
linux怎么使用正点原子无线dap烧录器
linux·运维·postgresql
coward911 小时前
Linux 内核 KGDB 以及内核驱动单串口调试笔记:telnet + agent-proxy + gdb-multiarch 实践
linux·单片机·嵌入式硬件