G
G
German2019-02-19 22:22:13
C++ / C#
German, 2019-02-19 22:22:13

async_accept return error in boost::asio?

I am writing an asynchronous TCP server, but when I start it, an "Acceptor Error" occurs, that is, an error is returned in accept_handler, what should I do?
error.message() says "The I/O operation was aborted due to command stream termination or application request."

#include <iostream>
#include <boost/asio.hpp>
#include <string>
#include <clocale>

using namespace boost::asio;
using namespace std;

class tcp_server
{
private:
    ip::tcp::socket socket;
    ip::tcp::acceptor acceptor;
    int clientCount = 0;
    int port = 0;
    enum { max_lenght = 256 };
    char readBuff[max_lenght];
    char writeBuff[max_lenght];
public:
    tcp_server(io_service& service, int port) : socket(service), acceptor(
        service,
        ip::tcp::endpoint(ip::tcp::v4(), port))
    {
        this->port = port;
        do_accept();
    }
    void do_accept()
    {
        acceptor.async_accept(
            socket,
            [this](const boost::system::error_code& error) {
            this->accept_handler(error);
        }
        );
    }
    void accept_handler(const boost::system::error_code& error)
    {
        if (!error)
        {
        }
        else
        {
            cout << "Ошибка акцептора\n";
            system("pause");
        }
    }
    void do_read()
    {
        if (socket.available())
        {
            socket.async_read_some(
                buffer(readBuff),
                [this](const boost::system::error_code& error, size_t bytes_transferred) {
                this->read_handler(error, bytes_transferred);
            }
            );
        }
    }
    void read_handler(const boost::system::error_code& error, size_t bytes_transferred)
    {
        if (!error)
        {
            strcpy_s(writeBuff, readBuff);
            socket.async_write_some(
                buffer(writeBuff),
                [this](const boost::system::error_code& error, size_t bytes_transferred) {
                this->write_handler(error, bytes_transferred);
            }
            );
        }
        else
        {
            cout << "Ошибка чтения из сокета\n";
            system("pause");
        }
    }
    void write_handler(const boost::system::error_code& error, size_t bytes_transferred)
    {
        if (!error)
        {

        }
        else
        {
            cout << "Ошибка записи в сокет\n";
            system("pause");
        }
    }
};

int main(int argc, char *argv[])
{
    try
    {
        setlocale(LC_ALL, "Rus");
        io_service service;
        tcp_server(service, 5000);
        service.run();
    }
    catch (exception& ex)
    {
        cout << "Исключение: " << ex.what();
        system("pause");
    }
    return 0;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
laphroaig, 2019-02-20
@laphroaig

tcp_server(service, 5000);
At you "server" is destroyed right after creation, the acceptor is closed, all operations are canceled.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question