Boost logo

Boost Users :

Subject: [Boost-users] [boost-user][asio]How can i limit tcpserver's connection count?
From: rhapsodyn (rhapsodyn_at_[hidden])
Date: 2011-03-15 08:32:31


here‘s the code

server:
class tcp_server
{
public:
    tcp_server(boost::asio::io_service& io_service)
        : acceptor_(io_service),limit(0)
    {
        tcp::endpoint endpoint(tcp::v4(), 10000);
        acceptor_.open(endpoint.protocol());
        acceptor_.bind(endpoint);
        acceptor_.listen(1);
        start_accept();
    }

private:
    void start_accept()
    {
        while(1)
        {
            if(limit < 1)
                break;
        }

        tcp::socket* socket = new tcp::socket(acceptor_.io_service());
        acceptor_.async_accept(*socket,
            boost::bind(
                &tcp_server::handle_accept,
                this,
                socket,
                boost::asio::placeholders::error));
    }

    void handle_accept(tcp::socket* s, const boost::system::error_code&
error)
    {
        if (!error)
        {
            ++limit;
            start_accept();
        }
    }

    tcp::acceptor acceptor_;

    int limit;
};

client:
int main(int argc, char* argv[])
{
    int i = 0;
    try
    {
        boost::asio::io_service io_service;

        tcp::resolver resolver(io_service);
        tcp::resolver::query query("127.0.0.1", "10000");
        tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
        tcp::endpoint endpoint = *endpoint_iterator;

        tcp::socket socket(io_service);
        socket.connect(endpoint);

        while(1)
        {}
    }
    catch (std::exception& e)
    {
        std::cerr << e.what() << std::endl;
    }

    return 0;
}

I thought i can only launch 1 client, but fact is: i can start two clients
and the 3rd one just cerr the what(), what happend there? Is there some
better way to get & limit the "living-connection" of the server?

--
View this message in context: http://boost.2283326.n4.nabble.com/boost-user-asio-How-can-i-limit-tcpserver-s-connection-count-tp3356439p3356439.html
Sent from the Boost - Users mailing list archive at Nabble.com.

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net