• muduo网络库编程


    muduo网络库简介

    muduo网络库给用户提供了两个主要的类
    TcpServer : 用户编写服务器程序的
    TcpClient : 用户编写客户端程序的

    优点:

    epoll + 线程池
    好处 : 能够把网络I/O的代码和业务代码区分开(用户的连接和断开 ,用户的可读写事件)

    基于muduo网络库开发服务器流程

    编写一个ChatServer为例:

    1. 组合TcpServer对象
    2. 创建EventLoop事件循环对象的指针
    3. 明确TcpServer构造函数需要哪些参数,输出ChatServer的构造函数
    4. 在当前服务器类的构造函数中,注册处理连接的回调函数和处理读写事件的回调函数
    5. 设置合适的服务端线程数量,muduo库会自己分配I/O线程和worker线程
    #include 
    #include 
    #include 
    #include 
    #include 
    using namespace std;
    using namespace muduo;
    using namespace muduo::net;
    using namespace placeholders;
    
    class ChatServer {
    public:
        ChatServer(EventLoop* loop,                 // 事件循环
                    const InetAddress& listenAddr,  // IP + Port
                    const string& nameArg)          // 服务器名字
            : _server(loop, listenAddr, nameArg), _loop(loop)
        {
            // 给服务器注册用户连接和创建和断开回调
            _server.setConnectionCallback(std::bind(&ChatServer::onConnection, this, _1));
    
            // 给服务器注册用户读写事件回调
            _server.setMessageCallback(std::bind(&ChatServer::onMessage, this, _1, _2, _3));
    
            // 设置服务端线程数量 1个I/O线程,3个worker线程
            _server.setThreadNum(4);
        }
    
        // 开启事件循环
        void start() {
            _server.start();
        }
    private:
        TcpServer _server;  // #1
        EventLoop* _loop;   // #2 epoll
    
        // 专门处理用户的连接创建和断开  epoll listenfd accept
        void onConnection(const TcpConnectionPtr& conn) {
            if (conn->connected()) {
                cout << conn->peerAddress().toIpPort() << " -> " <<
                    conn->localAddress().toIpPort() <<" state: online " << endl;
            }
            else {
                cout << conn->peerAddress().toIpPort() << " -> " <<
                    conn->localAddress().toIpPort() <<" state: offline " << endl;
    
                conn->shutdown();   // close(fd)
                // _loop->quit();
            }
        }
    
        // 专门处理用户的读写事件
        void onMessage(const TcpConnectionPtr& conn, // 连接
                        Buffer* buffer,                // 缓冲区
                        Timestamp time)             // 收到数据的时间信息
        {
            string buf = buffer->retrieveAllAsString();
            cout << "recv data: " << buf << "time: " << time.toString() << endl;
            conn->send(buf);
        }
    };
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62

    必须要指定构造函数,因为默认的需要四个参数
    在这里插入图片描述

    构造函数中编写回调函数,利用绑定器和专门处理事件的函数(和参数)绑定。
    在这里插入图片描述

    编译并链接相应的库
    在这里插入图片描述

    使用telnet检测连接
    在这里插入图片描述

    现在可以进行数据传输
    在这里插入图片描述

    退出示例:

    在这里插入图片描述

  • 相关阅读:
    GaussDB数据库SQL系列-定义重载函数
    2023昆明理工大学计算机考研信息汇总
    一元二次方程到规范场
    C语言——指针进阶(二)
    java毕业生设计在线直播平台计算机源码+系统+mysql+调试部署+lw
    软件测试也逃不过“35岁危机”?
    【ASM】字节码操作 工具类与常用类 LocalVariablesSorter 类 简单介绍与使用
    Vue页面内容未保存时离开页面做弹框提示
    安达发|印刷包装行业利用APS自动排产系统迎来绿色革命
    排队返利模式:开启消费者与商家的共赢新篇章
  • 原文地址:https://blog.csdn.net/m0_56257585/article/details/125891388