赞
踩
muduo是由Google大佬陈硕开发,是一个基于非阻塞IO和事件驱动的现代C++网络库,原生支持one loop per thread这种IO模型,该库只支持Linux系统,网上大佬对其褒贬不一,作为小白用来学习就无可厚非了。
git仓库:https://github.com/chenshuo/muduo
$ sudo apt-get install cmake
$ sudo apt-get install libboost-dev libboost-test-dev
$ sudo apt-get install libcur14-openssl-dev libc-ares-dev
$ sudo apt-get install protobuf-compiler libprotobuf-dev
// 切换路径
$ 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/
#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_; };
#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(); }
#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(); }
$ g++ EchoServer.cpp test.cpp -o test -lmuduo_base -lmuduo_net -lpthread
错误消息如下:
执行test.out程序启动服务端,再通过终端模拟客户端建立连接,在客户端发送消息会同时接收服务端回复的相同消息。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。