首页 > 其他 > 详细

evpp tcp server服务端

时间:2019-11-23 00:06:20      阅读:321      评论:0      收藏:0      [点我收藏+]
// netserver.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <evpp/tcp_server.h>
#include <evpp/buffer.h>
#include <evpp/tcp_conn.h>
#include <vector>
#include <list>
using namespace std;
#ifdef _WIN32
#include "winmain-inl.h"
#endif
std::vector<evpp::TCPConnPtr> conns;
std::vector<evpp::TCPConnPtr>::iterator iter;
void OnMessage(const evpp::TCPConnPtr& conn,
    evpp::Buffer* msg) {
    std::string s = msg->NextAllString();
    // LOG_INFO << "Received a message [" << s << "]";

    std::cout << conns.size() << std::endl;
    for (iter = conns.begin(); iter != conns.end(); iter++)
    {
        if (*iter != conn)
        {

            evpp::TCPConnPtr sconn;
            sconn = *iter;

            sconn->Send(s);
        }
        else
            continue;
    }

    if (s == "quit" || s == "exit") {
        conn->Close();
    }
}


void OnConnection(const evpp::TCPConnPtr& conn) {
    if (conn->IsConnected()) {
        std::cout << "Accept a new connection from " << conn->remote_addr();
        std::cout << conn->remote_addr() << std::endl;
        conns.push_back(conn);
    }
    else {
        std::cout << "Disconnected from " << conn->remote_addr();

        //  conns.pop(conn);
        conns.erase(remove(conns.begin(), conns.end(), conn), conns.end());

    }




}


int main(int argc, char* argv[]) {


    std::string port = "9099";

    conns.clear();
    
    std::string addr = std::string("0.0.0.0:") + port;
    evpp::EventLoop loop;
    evpp::TCPServer server(&loop, addr, "TCPEcho", 0);
    server.SetConnectionCallback(&OnConnection);
    server.SetMessageCallback(&OnMessage);

    server.Init();
    server.Start();
    loop.Run();
    return 0;
}

 

evpp tcp server服务端

原文:https://www.cnblogs.com/hshy/p/11914848.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!