muduo网络库编程
Linux环境下Epoll+pthread线程库
Boost库安装与使用
-
安装Boost库
下载boost库源码,linux环境解压
tar -zxvf boost_1_69_0.tar.gz
-
解压完成后,进入该目录,查看内容
-
运行bootstrap.sh工程编译构建程序
./bootstrap.sh
-
源码根目录下生成了b2程序,运行b2程序如下(boost源码比较大,这里编译需要花费一些时间):
【注意】:如果Linux系统没有安装g++编译器,需要先安装g++,建议g++4.6版本以上,能比较好的支持C++新标准,可以通过命令 g++ --version 查看g++版本号。
./b2
编译完成后
最后,再把上面的boost库头文件和lib库文件安装在默认的Linux系统头文件和库文件的搜索路径下,运行下面命令(因为要给/usr目录下拷贝文件,需要先进入root用户):
root@tony-virtual-machine:/home/tony/package/boost_1_69_0# ./b2 install
5. 测试编译
#include <iostream>
#include <boost/bind.hpp>
#include <string>
using namespace std;
class Hello
{
public:
void say(string name)
{ cout << name << " say: hello world!" << endl; }
};
int main()
{
Hello h;
auto func = boost::bind(&Hello::say, &h, "zhang san");
func();
return 0;
}
muduo的使用
muduo-master.zip
cd muduo-master
ls
vim编辑上面源码目录里面的CMakeLists.txt文件,如下修改:
执行./build.sh
编译完成后,输入./build.sh install 进行安装muduo库
这个./build.sh install实际上把muduo的头文件和lib库文件放到了muduo-master同级目录下的build目录下的release-install-cpp11文件夹下面了, install 会把muduo的头文件和lib库文件放到muduo-master同级目录下的build文件夹下~
echo ‘main(){}’|gcc -E -v - 查看include搜索路径,可以看到include搜索路径
install安装会导致我们每次编译程序都需要指定muduo库的头文件和库文件路径,很麻烦,所以我们选择直接把inlcude(头文件)和lib(库文件)目录下的文件拷贝到系统目录下:
root@tony-virtual-machine:/home/tony/package/build/release-install-cpp11# ls
include lib
root@tony-virtual-machine:/home/tony/package/build/release-install-cpp11# cd include/
root@tony-virtual-machine:/home/tony/package/build/release-install-cpp11/include# ls
muduo
root@tony-virtual-machine:/home/tony/package/build/release-install-cpp11/include# mv muduo/ /usr/include/
root@tony-virtual-machine:/home/tony/package/build/release-install-cpp11/include# cd ..
root@tony-virtual-machine:/home/tony/package/build/release-install-cpp11# ls
include lib
root@tony-virtual-machine:/home/tony/package/build/release-install-cpp11# cd lib/
root@tony-virtual-machine:/home/tony/package/build/release-install-cpp11/lib# ls
libmuduo_base.a libmuduo_http.a libmuduo_inspect.a libmuduo_net.a
root@tony-virtual-machine:/home/tony/package/build/release-install-cpp11/lib# mv * /usr/local/lib/
root@tony-virtual-machine:/home/tony/package/build/release-install-cpp11/lib#
拷贝完成以后使用muduo库编写C++网络程序,不用在指定头文件和lib库文件路径信息了,因为g++会自动从/usr/include和/usr/local/lib路径下寻找所需要的文件。
测试代码
#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();
}
g++ main.cpp -lmuduo_net -lmuduo_base -lpthread -std=c++11
等待客户端连接:
echo "hello world" | nc localhost 8888
muduo安装成功,能够正常进行C++网络程序开发~
参考博客