muduo C + + Network Library Learning

1, Installation of muduo Library

1. Install dependent files

muduo uses CMake as the build system, and the installation method is as follows:

  • Install cmake: sudo apt get install cmake

muduo relies on Boost and is easy to install:

  • Install the boost library sudo apt get install libboost dev libboost test dev

muduo has three non essential dependent libraries: curl, c-ares DNS and Google Protobuf. If these three libraries are installed, cmake will automatically compile more examples. The installation method is as follows:

  • sudo apt-get install libcurl4-openssl-dev libc-ares-dev
  • sudo apt-get install protobuf-compiler libprotobuf-dev
2. Install muduo Library

Muduo project address: https://github.com/chenshuo/muduo

  1. Download the released version of muduo.tar.gz on github: https://github.com/chenshuo/muduo/archive/refs/tags/v2.0.2.tar.gz
  2. The Muduo compilation method is very simple (take muduo-0.8.2-beta.tar.gz as an example):
    • Unzip tar zxf muduo-0.8.2-beta.tar.gz
    • Enter the muduo directory cd muduo/
    • Execute the shell script to compile the file. / build.sh -j2
      Compile the muduo library and its own examples, and generate executable files and static library files
      Located in... / build/debug/{bin,lib}
    • Install the compiled file. / build.sh install
      The above command installs the muduo header file and library file to... / build / debug install / {include, lib} for use by libraries such as muduo protorpc and muduo udns
  • If you want to compile the release version (optimized with - O2), you can execute:
    $ BUILD_TYPE=release ./build.sh -j2

  • Compile the muduo library and its own examples. The generated executable files and static library files are located in... / build/release/{bin,lib}

  • BUILD_TYPE=release ./build.sh install
    The above command installs the muduo header file and library file to... / build / release install / {include, lib} for use by libraries such as muduo protorpc and muduo udns

After the official release of muduo 1.0, build_ The default value of type will be changed to release.

After compiling, please try to run the examples, such as bin/inspector_test, and then access it through the browser http://10.0.0.10:12345/ or http://10.0.0.10:12345/proc/status , where 10.0.0.10 is replaced by the IP of your Linux box.

3. Use muduo in your own program

Muduo is a statically linked C + + program library. When using Muduo library, you only need to set the header file path (such as... / build / debug install / include) and library file path (such as... / build / debug install / LIB) and link the corresponding static library file (- lmuduo_net -lmuduo_base). The following demonstration project shows how to compile Muduo based programs using CMake and ordinary makefile s: https://github.com/chenshuo/muduo-tutorial

2, muduo programming example

The muduo library comes with some programming examples.

1. Implementation source code of echo service

echo.h

#ifndef MUDUO_EXAMPLES_SIMPLE_ECHO_ECHO_H
#define MUDUO_EXAMPLES_SIMPLE_ECHO_ECHO_H

#include "muduo/net/TcpServer.h"

// RFC 862
class EchoServer		// echo class design
{
 public:
  EchoServer(muduo::net::EventLoop* loop,
             const muduo::net::InetAddress& listenAddr);

  void start();  // calls server_.start();

 private:	// Call back this function when a connection is generated
  void onConnection(const muduo::net::TcpConnectionPtr& conn);
			// This function is called back when there is a message
  void onMessage(const muduo::net::TcpConnectionPtr& conn,
                 muduo::net::Buffer* buf,
                 muduo::Timestamp time);

  muduo::net::TcpServer server_;
};

#endif  // MUDUO_EXAMPLES_SIMPLE_ECHO_ECHO_H

echo.cc

#include "examples/simple/echo/echo.h"

#include "muduo/base/Logging.h"

//Placeholder_ 1 is used to replace the first parameter in the callback function_ 2 is used instead of the second parameter in the callback function..
using std::placeholders::_1;
using std::placeholders::_2;
using std::placeholders::_3;

// using namespace muduo;
// using namespace muduo::net;

EchoServer::EchoServer(muduo::net::EventLoop* loop,
                       const muduo::net::InetAddress& listenAddr)
  : server_(loop, listenAddr, "EchoServer")
{
  server_.setConnectionCallback(
      std::bind(&EchoServer::onConnection, this, _1));
  server_.setMessageCallback(
      std::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);
}

main.cc

#include "examples/simple/echo/echo.h"

#include "muduo/base/Logging.h"
#include "muduo/net/EventLoop.h"

#include <unistd.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();
}

Note: after the muduo library is compiled and installed, a build directory will be generated.
Store the executable programs and libraries generated after our compilation in this directory.

2.1.1 execute echo program (bin /...)

If we want to run the echo program, we can directly run simple in the muduo installation directory / build/release-cpp11/bin_ Just echo.

2.1.2 execute echo program (g + + compilation)

  • Where, - l specifies the library directory, installation directory / build/release-cpp11/lib, - I specifies the header file directory, installation directory / muduo-2.0.2 /, and links the library by using - l library name.
2.1.3 execution effect


Enter 0.0.0.0:2007 in the browser to simulate client access. The echo program has the following output:

Or use netcat to simulate the connection

The client sends "hello", and the echo service returns "hello" to the client after receiving it.

Client disconnected.

Tags: Linux network TCP/IP

Posted on Tue, 30 Nov 2021 09:34:33 -0500 by Nico