美文网首页
c++ sim库服务端示例

c++ sim库服务端示例

作者: 一路向后 | 来源:发表于2022-03-10 21:59 被阅读0次

    1.源码实现

    #include <iostream>
    #include <sim/sim.h>
    
    using namespace std;
    
    class MyHandler : public sim::Handler {
    public:
        virtual sim::HandlerState proc(const sim::Request &req, sim::Response *resp)
        {
            std::string cmd = req.msg.type();
    
            if(cmd == "ping")
            {
                resp->msg.add("ok");
                resp->msg.add("pong");
            }
            else
            {
                resp->msg.add("ok");
                resp->msg.add("cmd");
            }
    
            return this->resp();
        }
    };
    
    int main()
    {
        const char *ip = "localhost";
        int port = 8080;
        sim::Server *serv = sim::Server::listen(ip, port);
    
        MyHandler handler;
    
        serv->add_handler(&handler);
    
        serv->loop();
    
        return 0;
    }
    

    2.编译源码

    $ g++ -o test test.cpp -std=c++11 -lsim
    

    3.运行及其结果

    $ ./test
    $ telnet localhost 8080
    Trying 127.0.0.1...
    Connected to localhost.
    Escape character is '^]'.
    ping
    0=ok 1=pong
    pong
    0=ok 1=cmd
    ^]
    telnet> quit
    Connection closed.
    

    相关文章

      网友评论

          本文标题:c++ sim库服务端示例

          本文链接:https://www.haomeiwen.com/subject/szpqdrtx.html