mongoose TCP Client ,Server同时存在,同时绑定多个端口,访问多个Server端

#include "network/mongoose.h"

static void ev_handler(struct mg_connection *nc, int ev, void *p) {
    struct mbuf *io = &nc->recv_mbuf;
    (void) p;

    switch (ev) {
    case MG_EV_RECV:
    {
        std::string str(io->buf,0,io->len);
        qDebug()<buf, io->len);  // Echo message back
        mbuf_remove(io, io->len);       // Discard message from recv buffer
        break;
    }
    default:
        break;
    }
}

int test() {
    struct mg_mgr mgr;
    std::string port[2] ={"6000","6001"};
    std::string url[2] ={"tcp://127.0.0.1:6002","tcp://127.0.0.1:6003"};
    mg_mgr_init(&mgr, NULL);
    mg_bind(&mgr, port[0].c_str(), ev_handler);//监听 6000端口
    mg_bind(&mgr, port[1].c_str(), ev_handler);//监听 6001端口
    mg_connect(&mgr,url[0].c_str(), ev_handler);//连接 127.0.0.1:6002 Server
    mg_connect(&mgr,url[1].c_str(), ev_handler);//连接 127.0.0.1:6003 Server
    printf("Starting echo mgr on ports %s, %s\n", port[0].c_str(), port[1].c_str());
    for (;;) {
        mg_mgr_poll(&mgr, 1000);
    }
    mg_mgr_free(&mgr);

    return 0;
}

你可能感兴趣的:(C++,HTTP)