如何在接受器循环中从ENFILE中恢复

时间:2015-01-26 08:28:15

标签: c++ boost-asio

我已经编写了一个测试来显示asio async_accept的问题 我有一台服务器,可以永久打开任何连接 在接受了许多连接(在我的情况下是1017)之后 连接失败,出现“打开文件太多”错误。 然后对async_accept的任何调用立即调用处理程序

这是误会吗?

我使用debian 7 amd64

生成文件

CXX=clang++ -O2
OBJ= main.o server.o
LIBS=-lboost_system -lboost_thread
all: server

server: $(OBJ)
    $(CXX) -o server $(OBJ) $(LIBS)

main.o: main.cpp
    $(CXX) -c main.cpp

server.o: server.hpp server.cpp
    $(CXX) -c server.cpp

clean:
    rm -f *.o *~

distclean: clean
    rm -f server

server.hpp

#include <boost/asio.hpp>

class server
{
    public:
        server();

        void run();

    private:
        void start_accept();

        void handle_accept(const boost::system::error_code& e);

        boost::asio::io_service ios;

        boost::asio::ip::tcp::acceptor acceptor_;

        std::vector<boost::asio::ip::tcp::socket*> sockets;
};

server.cpp

#include <boost/bind.hpp>
#include "server.hpp"

server::server(): acceptor_(ios)
{
    boost::asio::ip::tcp::resolver resolver(ios);
    boost::asio::ip::tcp::resolver::query query("0.0.0.0", "5050");
    boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(query);
    acceptor_.open(endpoint.protocol());
    acceptor_.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
    acceptor_.bind(endpoint);
    acceptor_.listen();

    start_accept();
}

void server::run()
{
    ios.run();
}

void server::start_accept()
{
    boost::asio::ip::tcp::socket *s=new  boost::asio::ip::tcp::socket(ios);
    sockets.push_back(s);
    acceptor_.async_accept(*s,
            boost::bind(&server::handle_accept, this,
                boost::asio::placeholders::error));
}

void server::handle_accept(const boost::system::error_code& e)
{
    if(e)
    {
        std::cerr<<"e.message() = "<<e.message()<<std::endl;
        boost::asio::ip::tcp::socket *s=sockets.back();
        s->close();
        delete s;
        sockets.pop_back();
    }
    static int i=1;
    std::cerr<<"i = "<<i++<<std::endl;
    start_accept();
}

的main.cpp

#include "server.hpp"

int main()
{
    server s;
    s.run();
    return 0;
}

我的测试是

for x in `seq $1`;do nc 127.0.0.1 5050 & done

2 个答案:

答案 0 :(得分:1)

server::handle_accept结束时,即使套接字出错,也会将异步接受提交回IO服务队列。

我不确定这是你想要的,但我可以做到&#34;工作&#34; (恢复)通过在接受失败时重新启动监听器。 (请注意,如果在多个线程上运行服务,则需要进行一些同步。)

<强> Live On Coliru

#include <boost/asio.hpp>

class server
{
    public:
        server();

        void run();

    private:
        bool start_listen();
        void start_accept();
        void handle_accept(boost::system::error_code e);

        boost::asio::io_service ios;
        boost::asio::ip::tcp::acceptor acceptor_;
        std::vector<boost::asio::ip::tcp::socket*> sockets;
};

#include <boost/bind.hpp>

server::server(): acceptor_(ios)
{
    start_listen();
    start_accept();
}

void server::run()
{
    ios.run();
}

bool server::start_listen()
{
    boost::system::error_code e;
    boost::asio::ip::tcp::endpoint endpoint { {}, 5050 };
    acceptor_.open(endpoint.protocol());
    acceptor_.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
    acceptor_.bind(endpoint, e);
    if (e)
        return false;
    acceptor_.listen();
    return true;
}

void server::start_accept()
{
    boost::asio::ip::tcp::socket *s=new  boost::asio::ip::tcp::socket(ios);
    sockets.push_back(s);
    acceptor_.async_accept(*s, boost::bind(&server::handle_accept, this, boost::asio::placeholders::error));
}

#include <iostream>

void server::handle_accept(boost::system::error_code e)
{
    if(e)
    {
        std::cerr<<"e.message() = "<<e.message()<<std::endl;
        boost::asio::ip::tcp::socket *s=sockets.back();
        s->close();
        delete s;
        sockets.pop_back();

        acceptor_.close();
        if (!start_listen())
            return;
    }
    static int i=1;
    std::cerr<<"i = "<<i++<<std::endl;
    start_accept();
}

int main()
{
    server s;
    s.run();
}

答案 1 :(得分:0)

以这种方式打开套接字

boost::asio::ip::tcp::socket *s=new  boost::asio::ip::tcp::socket(ios);

套接字未打开,只创建了一个对象。当您尝试接受它时,您会达到打开的文件描述符限制,因此您会收到“打开文件太多”错误。然后你只需从vector中删除它,然后创建另一个无法在accept中打开的套接字。我认为这是错误的原因。

更新: 顺便说一下,这个功能

void server::start_accept()
{
    boost::asio::ip::tcp::socket *s=new  boost::asio::ip::tcp::socket(ios);
    sockets.push_back(s);
    acceptor_.async_accept(*s,
        boost::bind(&server::handle_accept, this,
            boost::asio::placeholders::error));
}

似乎有可能的内存泄漏,如果在push_back方法中生成异常,s将丢失。

更新:如果以这种方式重写handle_accept,则修复inifinte循环:

void server::handle_accept(const boost::system::error_code& e)
{
    if(e)
    {
      auto size = 2;
      for (int i = 0; i < size; ++i) {
        std::cerr<<"e.message() = "<<e.message()<<std::endl;
        boost::asio::ip::tcp::socket *s=sockets.back();
        s->close();
        delete s;
        sockets.pop_back();
      }
    }
    static int i=1;
    std::cerr<<"i = "<<i++<<std::endl;
    start_accept();
}