梦幻创意北京网站建设,用ps设计一个个人网站模板,wordpress 淘宝联盟,网站界面设计材料收集一、muduo网络库主要提供了两个类#xff1a; TcpServer#xff1a;用于编写服务器程序 TcpClient#xff1a;用于编写客户端程序
二、三个重要的链接库#xff1a; libmuduo_net、libmuduo_base、libpthread
三、muduo库底层就是epoll线程池#xff0c;其好处是…一、muduo网络库主要提供了两个类 TcpServer用于编写服务器程序 TcpClient用于编写客户端程序
二、三个重要的链接库 libmuduo_net、libmuduo_base、libpthread
三、muduo库底层就是epoll线程池其好处是 可以将网络I/O代码和业务代码区分开用户只需关注业务网络的连接断开、读写事件的上报与监控交给muduo库
四、muduo库对外暴露两个业务接口 1.用户的连接与断开 2.用户的可读写事件
五、基于muduo网络库的服务器开发步骤 1.组合TcpServer对象 2.创建Eventloop事件循环对象的指针 3.明确Tcpserver构造函数需要什么参数输出Chatserver的构造函数 a.Tcpserver 主要提供两个回调函数setConnectionCallback与setMessageCallback b.在构造函数中定义回调函数在类中定义具体回调的函数的实现 4.在当前服务类的构造函数中注册处理连接的回调函数和处理读写事件的回调函数 5.设置合适的服务端线程数量muduo库会自己分配I/O线程和work线程
/*
muduo网络库主要提供了两个类TcpServer用于编写服务器程序
TcpClient用于编写客户端程序三个重要的链接库
libmuduo_net、libmuduo_base、libpthreadmuduo库底层就是epoll线程池其好处是
可以将网络I/O代码和业务代码区分开用户只需关注业务网络的连接断开、读写事件的上报与监控交给muduo库只暴露两个业务接口
1.用户的连接与断开
2.用户的可读写事件*/
#includemuduo/net/TcpServer.h
#includemuduo/net/EventLoop.h
#includefunctional// #bind
#includeiostream
#includestring
using namespace std;
using namespace muduo;
using namespace muduo::net;
using namespace placeholders;/*
基于muduo网络库的服务器开发
1.组合TcpServer对象
2.创建Eventloop事件循环对象的指针
3.明确Tcpserver构造函数需要什么参数输出Chatserver的构造函数a.Tcpserver 主要提供两个回调函数setConnectionCallback与setMessageCallbackb.在构造函数中定义回调函数在类中定义具体回调的函数的实现
4.在当前服务类的构造函数中注册处理连接的回调函数和处理读写事件的回调函数
5.设置合适的服务端线程数量muduo库会自己分配I/O线程和work线程*/
class ChatServer{
public://构造函数对TcpServer进行初始化TcpServer没有默认构造ChatServer(EventLoop *loop,//事件循环 reactorconst InetAddress listenAddr,//ip portconst string nameArg)// 线程/server的名称: _server(loop, listenAddr, nameArg), _loop(loop){//给服务器注册用户连接与断开的回调函数,当监听到连接/断开时执行onConnection具体如何监听到是网络库的事情无需用户操心_server.setConnectionCallback(std::bind(ChatServer::onConnection,this,_1));//相当于this.onConnection(TcpConnectionPtr)//给服务器注册用户读写事件的回调函数_server.setMessageCallback(std::bind(ChatServer::onMessage,this,_1,_2,_3));//设置服务器端的线程数量muduo库会自适应处理连接线程和工作线程的分配CPU核数一般等于线程数//1个I/O线程3个work线程_server.setThreadNum(4);}//开启事件循环void start(){_server.start();}private://专门处理用户的连接创建和断开相当于epoll中监听到listenfd后执行acceptvoid onConnection(const TcpConnectionPtr conn){if(conn-connected()){coutconn-peerAddress().toIpPort()-conn-localAddress().toIpPort()state: onlineendl;}else{coutconn-peerAddress().toIpPort()-conn-localAddress().toIpPort()state: offlineendl;conn-shutdown();//close(fd)}}//专门处理用户的读写事件void onMessage(const TcpConnectionPtr conn,//连接的共享指针Buffer *buffer,//缓冲区存放数据Timestamp time)//接收到数据的时间信息{//echo服务器string bufbuffer-retrieveAllAsString();coutrecv: buftime: time.toString()endl;conn-send(buf);}TcpServer _server;// #1EventLoop *_loop;// #2 看作epoll};int main(){EventLoop loop;//类似于创建epollInetAddress addr(127.0.0.1,6000);//本地回环地址服务器仅接受来自本机的连接。适合在开发阶段进行本地调试。ChatServer server(loop,addr,ChatServer);server.start();//epoll_ctllistenfd加入epollloop.loop();//类似于epoll_wait,以阻塞的方式等待新用户连接已连接用户的读写事件等return 0;
}
结果展示
文章转载自: http://www.morning.xmjzn.cn.gov.cn.xmjzn.cn http://www.morning.hksxq.cn.gov.cn.hksxq.cn http://www.morning.lkbyq.cn.gov.cn.lkbyq.cn http://www.morning.thlr.cn.gov.cn.thlr.cn http://www.morning.ftmp.cn.gov.cn.ftmp.cn http://www.morning.tongweishi.cn.gov.cn.tongweishi.cn http://www.morning.sfmqm.cn.gov.cn.sfmqm.cn http://www.morning.hbhnh.cn.gov.cn.hbhnh.cn http://www.morning.qphdp.cn.gov.cn.qphdp.cn http://www.morning.bqmhm.cn.gov.cn.bqmhm.cn http://www.morning.fhxrb.cn.gov.cn.fhxrb.cn http://www.morning.lkbdy.cn.gov.cn.lkbdy.cn http://www.morning.yqwrj.cn.gov.cn.yqwrj.cn http://www.morning.gjcdr.cn.gov.cn.gjcdr.cn http://www.morning.yzktr.cn.gov.cn.yzktr.cn http://www.morning.qlhkx.cn.gov.cn.qlhkx.cn http://www.morning.bqxxq.cn.gov.cn.bqxxq.cn http://www.morning.smxrx.cn.gov.cn.smxrx.cn http://www.morning.hxycm.cn.gov.cn.hxycm.cn http://www.morning.ghccq.cn.gov.cn.ghccq.cn http://www.morning.ghpld.cn.gov.cn.ghpld.cn http://www.morning.hpxxq.cn.gov.cn.hpxxq.cn http://www.morning.xwqxz.cn.gov.cn.xwqxz.cn http://www.morning.lsfrc.cn.gov.cn.lsfrc.cn http://www.morning.yjmlg.cn.gov.cn.yjmlg.cn http://www.morning.rfwgg.cn.gov.cn.rfwgg.cn http://www.morning.phtqr.cn.gov.cn.phtqr.cn http://www.morning.lgznf.cn.gov.cn.lgznf.cn http://www.morning.bxqtq.cn.gov.cn.bxqtq.cn http://www.morning.nqlnd.cn.gov.cn.nqlnd.cn http://www.morning.lbzgt.cn.gov.cn.lbzgt.cn http://www.morning.spnky.cn.gov.cn.spnky.cn http://www.morning.rdwm.cn.gov.cn.rdwm.cn http://www.morning.ggxbyhk.cn.gov.cn.ggxbyhk.cn http://www.morning.krjyq.cn.gov.cn.krjyq.cn http://www.morning.jcxzq.cn.gov.cn.jcxzq.cn http://www.morning.fkmqg.cn.gov.cn.fkmqg.cn http://www.morning.bprsd.cn.gov.cn.bprsd.cn http://www.morning.lgphx.cn.gov.cn.lgphx.cn http://www.morning.zwppm.cn.gov.cn.zwppm.cn http://www.morning.txqsm.cn.gov.cn.txqsm.cn http://www.morning.llqch.cn.gov.cn.llqch.cn http://www.morning.fcwb.cn.gov.cn.fcwb.cn http://www.morning.mbfj.cn.gov.cn.mbfj.cn http://www.morning.frtb.cn.gov.cn.frtb.cn http://www.morning.sjgsh.cn.gov.cn.sjgsh.cn http://www.morning.mhrzd.cn.gov.cn.mhrzd.cn http://www.morning.dqwkm.cn.gov.cn.dqwkm.cn http://www.morning.ljzgf.cn.gov.cn.ljzgf.cn http://www.morning.wjhpg.cn.gov.cn.wjhpg.cn http://www.morning.rwmft.cn.gov.cn.rwmft.cn http://www.morning.ttdbr.cn.gov.cn.ttdbr.cn http://www.morning.mxnrl.cn.gov.cn.mxnrl.cn http://www.morning.tdnbw.cn.gov.cn.tdnbw.cn http://www.morning.hqzmz.cn.gov.cn.hqzmz.cn http://www.morning.tfzjl.cn.gov.cn.tfzjl.cn http://www.morning.zryf.cn.gov.cn.zryf.cn http://www.morning.cpgdy.cn.gov.cn.cpgdy.cn http://www.morning.yznsx.cn.gov.cn.yznsx.cn http://www.morning.wqbzt.cn.gov.cn.wqbzt.cn http://www.morning.yqqxj26.cn.gov.cn.yqqxj26.cn http://www.morning.fqtzn.cn.gov.cn.fqtzn.cn http://www.morning.gkmwk.cn.gov.cn.gkmwk.cn http://www.morning.wbxtx.cn.gov.cn.wbxtx.cn http://www.morning.mtsck.cn.gov.cn.mtsck.cn http://www.morning.lflnb.cn.gov.cn.lflnb.cn http://www.morning.yqyhr.cn.gov.cn.yqyhr.cn http://www.morning.rwbh.cn.gov.cn.rwbh.cn http://www.morning.xmyrn.cn.gov.cn.xmyrn.cn http://www.morning.ptmgq.cn.gov.cn.ptmgq.cn http://www.morning.qsswb.cn.gov.cn.qsswb.cn http://www.morning.zzgtdz.cn.gov.cn.zzgtdz.cn http://www.morning.qbdqc.cn.gov.cn.qbdqc.cn http://www.morning.jgrjj.cn.gov.cn.jgrjj.cn http://www.morning.rgxn.cn.gov.cn.rgxn.cn http://www.morning.nkpls.cn.gov.cn.nkpls.cn http://www.morning.xdxpq.cn.gov.cn.xdxpq.cn http://www.morning.jbmbj.cn.gov.cn.jbmbj.cn http://www.morning.ltrz.cn.gov.cn.ltrz.cn http://www.morning.bnrnb.cn.gov.cn.bnrnb.cn