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.pswp.cn/news/716443.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/716443.shtml
英文地址,請注明出處:http://en.pswp.cn/news/716443.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

b站小土堆pytorch學習記錄——P14 torchvision中的數據集使用

文章目錄 一、前置知識如何查看torchvision的數據集 二、代碼&#xff08;附注釋&#xff09;及運行結果 一、前置知識 如何查看torchvision的數據集 &#xff08;1&#xff09;打開官網 https://pytorch.org/ pytorch官網 &#xff08;2&#xff09;打開torchvision 在Do…

Linux:top顯示信息

簡介 top命令是Linux系統中常用的性能監控工具&#xff0c;它可以實時顯示系統中各個進程的CPU使用情況以及其他系統資源的使用情況。當您在終端中運行top命令時&#xff0c;它會顯示一個實時更新的列表。 CPU相關的信息 CPU狀態&#xff1a; us&#xff08;user&#xff09…

梯度和梯度損失

梯度主要用于計算圖像融合過程中的梯度損失&#xff0c;對應的損失函數是梯度損失&#xff08;loss_grad&#xff09;。 梯度的作用&#xff1a; 圖像的梯度是指圖像中每個像素的灰度變化率&#xff0c;通常用于表示圖像的邊緣和紋理信息。在圖像融合任務中&#xff0c;通過計算…

Unity游戲輸入系統(新版+舊版)

使用新版還是舊版 舊版 using System.Collections; using System.Collections.Generic; using UnityEngine;public class c5 : MonoBehaviour {void Start(){}void Update(){// 注意要在游戲中 點鼠標鍵盤進行測試// 鼠標// 0左鍵 1右鍵 2滾輪if (Input.GetMouseButtonDown(0)…

【javaSE-語法】lambda表達式

【javaSE-語法】lambda表達式 1. 先回憶一下&#xff1a;1.1 接口不能直接通過關鍵字new進行實例化1.2 函數式接口1.3 匿名內部類1.31 匿名內部類在代碼中長啥樣&#xff1f;1.32 構造一個新的對象與構造一個擴展了某類的匿名內部類的對象&#xff0c;兩者有什么區別&#xff1…

maven--->maven中的<properties>屬性有什么作用?

&#x1f64c;&#x1f64c;&#x1f64c;&#x1f64c;&#x1f64c;&#x1f64c; 在Maven中&#xff0c;元素用于定義項目中可重用的屬性值。這些屬性值可以在項目的POM文件中被引用&#xff0c;以便在整個項目中統一管理和使用。通過使用元素&#xff0c;可以避免在POM文件…

SpringBean生命周期之InitializingBean,初始化bean

1 yml文件 weixin:appid: aaaaaapartner: 12313214partnerkey: ccccccert: C:\\Users\\lenovo\\Desktop 2 Bean初使化 import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Value; import org.springframewor…

midjourney提示詞語法

更高級的提示可以包括一個或多個圖像URL、多個文本短語和一個或更多個參數 Image Prompts 可以將圖像URL添加到提示中&#xff0c;以影響最終結果的樣式和內容。圖像URL總是位于提示的前面。 https://docs.midjourney.com/image-prompts Text Prompt 要生成的圖像的文本描述。…

YOLOv6、YOLOv7、YOLOv8網絡結構圖(清晰版)

承接上一篇博客&#xff1a;YOLOv3、YOLOv4、YOLOv5、YOLOx的網絡結構圖(清晰版)_yolox網絡結構圖-CSDN博客 1. YOLOv6網絡結構圖 2. YOLOv7網絡結構圖 3. YOLOv8網絡結構圖

搭建 LNMP 架構

一 理論知識 &#xff08;一&#xff09;架構圖 &#xff08;二&#xff09;CGI 由來 最早的Web服務器只能簡單她響應瀏覽器發來的HTTP請求&#xff0c;并將存儲在服務器上的HTML文件返回給瀏覽器&#xff0c;也就是靜態html文件&#xff0c;但是后期隨著網站功能增多網站開…

c++階梯之模板初階

1. 泛型編程 void Swap(int& x, int& y) {int tmp x;x y;y tmp; }void Swap(double& x, double& y) {double tmp x;x y;y tmp; }void Swap(char& x, char& y) {char tmp x;x y;y tmp; } int main() {int a 10, b 20;double c 1.1, d 2.2…

《Spring Security 簡易速速上手小冊》第7章 REST API 與微服務安全(2024 最新版)

文章目錄 7.1 保護 REST API7.1.1 基礎知識詳解7.1.2 重點案例&#xff1a;使用 JWT 進行身份驗證和授權案例 Demo 7.1.3 拓展案例 1&#xff1a;API 密鑰認證案例 Demo測試API密鑰認證 7.1.4 拓展案例 2&#xff1a;使用 OAuth2 保護 API案例 Demo測試 OAuth2 保護的 API 7.2 …

讀書筆記-三國演義-夏侯惇

夏侯惇&#xff08;公元夏侯惇&#xff08;公元190年-公元220年&#xff09;&#xff0c;字元讓&#xff0c;沛國譙縣&#xff08;今安徽亳州市&#xff09;人&#xff0c;是中國東漢末年至三國時期魏國重要將領之一。他是曹操麾下的得力將領&#xff0c;以勇猛忠誠而聞名于世。…

linux安裝matlab獲取許可證

1.點擊許可證 2. 3. 4. 4.主機ID 打開linux輸入 /sbin/ifconfigether后邊的就是 6.計算機登錄名 打開linux輸入 whoami7. 8. 9.

局域網https自簽名教程

們的客戶是在內網環境里面&#xff0c;所以就只能用自簽名證書來搞&#xff0c;我一想這還不容易&#xff0c;就迅速的百度了一下隨便找了個文章開始照貓畫虎&#xff0c;很快就弄完了&#xff0c;但是弄完后發現還是有問題&#xff0c;而且https 還是報不安全&#xff0c; 1、…

(規劃)24屆春招和25屆暑假實習路線準備規劃

春招&&暑假實習&#xff1a; 1.八股&#xff1a; 可以去一些八股網站上面進行閱讀。 2.項目&#xff1a;至少準備1-2個項目&#xff0c;可以條理清晰的進行項目介紹和難點剖析。 3.算法&#xff1a; hot100 &#xff0c;劍指offer 能刷的很熟&#xff0c;算法關就差…

【R包報錯】使用sceasy包轉換rds文件與h5ad文件報錯PyType_GetFlags

想要將scanpy的h5ad文件轉為rds文件&#xff0c;有一個很方便的包sceasy可以使用&#xff0c;安裝簡單&#xff0c;代碼簡易。 安裝方式 # bioconda package:conda install -c bioconda r-sceasy# R package:devtools::install_github("cellgeni/sceasy")#額外需要…

android系統開發之--ROM編譯,repo使用指南

1、檢出代碼&#xff0c;指定git指定的分支和manifest repo init -u git://xxx/xxx/manifest.git -b <BRANCH> -m <MANIFEST> 這里-m和-b的含義是&#xff1a; 1. 注意到manifest.git本身也是一個git project 2. 所以&#xff0c;-b指定的是使用#1中這個git proj…

我的相關獎項

博士錄取證明 名單第53&#xff1a;https://yzb.bupt.edu.cn/content/content.php?p2_2_651 論文鏈接 第一篇&#xff1a;https://doi.org/10.1186/s13677-022-00373-8 第二篇&#xff1a;https://doi.org/10.1016/j.ipm.2022.103167 第三篇&#xff1a;https://doi.org/10…

Unity 腳本-生命周期常用函數

在Unity中&#xff0c;萬物皆是由組件構成的。 右鍵創建C&#xff03;腳本&#xff0c;拖動腳本到某物體的組件列表。 生命周期相關函數 using System.Collections; using System.Collections.Generic; using UnityEngine;// 必須要繼承 MonoBehaviour 才是一個組件 // 類名…