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

测试结果如下:

相关推荐
YJlio4 小时前
Active Directory 工具学习笔记(10.8):AdInsight——保存与导出(证据留存、共享与二次分析)
数据库·笔记·学习
_w_z_j_5 小时前
Linux----mmap
linux
程序员zgh6 小时前
Linux系统常用命令集合
linux·运维·服务器·c语言·开发语言·c++
獭.獭.6 小时前
C++ -- STL【unordered_set与unordered_map的实现】
开发语言·c++·unordered_map·unordered_set
Bigan(安)6 小时前
【奶茶Beta专项】【LVGL9.4源码分析】09-core-obj_class对象类系统
linux·c语言·mcu·arm·unix
qq_433554547 小时前
C++数位DP
c++·算法·图论
紫郢剑侠7 小时前
飞秋@Windows +iptux@Linux,打造内网跨平台IM环境
linux·运维·服务器·im·qq
保持低旋律节奏7 小时前
linux——调试
linux·运维·服务器
噗噗夹的TA之旅7 小时前
Unity Shader 学习20:URP LitForwardPass PBR 解析
学习·unity·游戏引擎·图形渲染·技术美术
牛奶咖啡137 小时前
Linux系统故障排查思路实践教程(下)
linux·运维·服务器·su命令切换用户问题解决·文件打开过多问题解决·linux网络故障问题解决·linux故障排查思路