write_some: Broken pipe C++

  boost-asio, c++

I have an issue with boost::asio. I’m trying to create basic chat, but have face write/write_some: Broken pipe error. Here’s code

Client side

Message here it is empty vector<char> message, data = "Client"
createMessage(message, data) put data into message

void Client::connect()
{
    int port = 9999;
    boost::asio::io_context ioc;
    boost::system::error_code ec;
    boost::asio::ip::tcp::endpoint address(boost::asio::ip::address::from_string("127.0.0.1"), port);

    boost::asio::ip::tcp::socket socket(ioc);
    socket.connect(address, ec);
    if(ec)
    {
        std::cerr<<ec<<std::endl;
    }
    for(;;)
    {

        createMessage(message, data);
        // VARIANT 1
        boost::asio::write(socket, boost::asio::buffer(message.data(), message.size()));

        // VARIANT 2
        //socket.write_some(boost::asio::buffer(message));    
    }
    socket.shutdown(boost::asio::socket_base::shutdown_both);
}

Server side

My server run on localhost with port 9999.

void Server::start()
{    
    boost::system::error_code ec;
    std::vector<char> buffer;
    buffer.reserve(2 * 1024);
    boost::asio::ip::tcp::endpoint address(boost::asio::ip::tcp::v4(), port);
    boost::asio::ip::tcp::acceptor acc(ioc, address);
    for(;;)
    {
        boost::asio::ip::tcp::socket socket(ioc);
        acc.accept(socket);
        if(ec)
        {
             std::cerr<<ec<<std::endl;
             return;
        }
        socket.read_some(boost::asio::buffer(buffer.data(), buffer.size()));
        for(auto& i : buffer)
        {
            std::cout<<i<<std::flush;
        }
    }

}

Can you give any clue what is wrong with it?

Source: Windows Questions C++

LEAVE A COMMENT