boost::asio::ip::tcp::socket doesn't read anything

xiver77

This is actually my first program in socket programming other than copy-pasting the tutorial code and having fun. Anyway it does not work. I think I have carefully read the documentation but maybe it wasn't enough. I'm suspecting my use of socket::read_some() in read_message() since my problematic program stops at the reading part. I thought the way I used it should be okay because the documentation of socket::read_some() stated that "the function call will block until one or more bytes of data has been read successfully, or until an error occurs." Below is my code. Any help appreciated.

void read_message(std::string& message, boost::asio::ip::tcp::socket& socket)
{
    std::stringstream message_stream;
    while (true)
    {
        std::array<char, 128> buffer;
        boost::system::error_code error;
        size_t len = socket.read_some(boost::asio::buffer(buffer), error);
        if (error == boost::asio::error::eof)
        {
            break;
        }
        message_stream.write(buffer.data(), len);
    }
    message = message_stream.str();
}

server.cpp

int main()
{
    while (true)
    {
        boost::asio::io_service io_service;
        boost::asio::ip::tcp::acceptor acceptor(io_service, boost::asio::ip::tcp::endpoint(
            boost::asio::ip::tcp::v4(), 9999));
        boost::asio::ip::tcp::socket socket(io_service);
        acceptor.accept(socket);
        std::cout << "connected with client, waiting for a message\n";
        std::string message;
        read_message(message, socket);
        boost::asio::ip::tcp::endpoint endpoint = socket.remote_endpoint();
        std::cout << "message received from " << endpoint.address().to_string() <<
            ':' << endpoint.port() << '\n' << message << '\n';
        boost::asio::write(socket, boost::asio::buffer(message));
        if (message.compare("quit") == 0)
        {
            break;
        }
    }
    return EXIT_SUCCESS;
}

client.cpp

int main()
{
    while (true)
    {
        std::cout << "your message to be sent: ";
        std::string message;
        std::getline(std::cin, message);
        boost::asio::io_service io_service;
        boost::asio::ip::tcp::resolver resolver(io_service);
        boost::asio::ip::tcp::resolver::query query("localhost", "9999");
        boost::asio::ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
        boost::asio::ip::tcp::socket socket(io_service);
        boost::asio::connect(socket, endpoint_iterator);
        std::cout << "connected to server\n";
        boost::asio::write(socket, boost::asio::buffer(message));
        read_message(message, socket);
        std::cout << "message received from server: " << message << '\n';
        if (message.compare("quit") == 0)
        {
            break;
        }
    }
    return EXIT_SUCCESS;
}
Igor R.

The problem is in read_message function: you read until eof, but you'll get eof only when the socket gets closed.

Perhaps you meant to return from read_message when some delimiter is encountered, eg. '\n'? Then you can either inspect the buffer manually, or use read_until function.

Using read_until function, read_message would look like this:

void read_message(std::string& message, boost::asio::ip::tcp::socket& socket)
{
  std::stringstream message_stream;
  boost::asio::streambuf buffer;
  boost::system::error_code error;
  size_t len = read_until(socket, buffer, '\n', error);
  if (len)
  {
    message_stream.write(boost::asio::buffer_cast<const char *>(buffer.data()), len);
    message = message_stream.str();
  }
}

Remember to append '\n' to the message you send from the client:

//...
std::cout << "connected to server\n";
boost::asio::write(socket, boost::asio::buffer(message + '\n'));
read_message(message, socket);
//...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

boost::asio::read throws compilation error 'read_some' is not a member of 'boost::shared_ptr<boost::asio::ip::tcp::socket>'

From Dev

Move constructor is not called with boost::asio::ip::tcp::socket

From Dev

Move constructor is not called with boost::asio::ip::tcp::socket

From Dev

Writing sections of data to TCP socket with boost::asio::ip::tcp from streambuf

From Dev

HTTP GET request with a TCP socket doesn't return anything

From Dev

Boost::ASIO: using tcp::socket on multiple threads

From Dev

Boost::ASIO: using tcp::socket on multiple threads

From Dev

C++11 thread crash when access boost::asio::ip::tcp::socket

From Dev

Read Binary Data over TCP with Boost::Asio

From Dev

Boost Asio type to use for both unix-socket and tcp socket

From Dev

How do I send a POST with boost::asio::ip::tcp::iostream?

From Dev

Write from asio::ip::tcp::socket directly to std::string

From Dev

boost::asio::read prevents boost:asio::write from sending data to Java Socket

From Dev

boost::asio read n bytes from socket to streambuf

From Dev

Boost.Asio: socket::close not cancelling aysnc_read_some

From Dev

Can't set TCP source Port with boost asio

From Dev

Why doesn't file read output anything?

From Dev

Boost Asio incomplete write to socket

From Dev

Boost.Asio TCP moved-to socket destructor not enough to cleanly close?

From Dev

Android TCP socket doesn't receive data

From Dev

boost::asio::deadline_timer doesn't call handler

From Dev

boost::asio::deadline_timer doesn't call handler

From Dev

boost::asio::ip::tcp::acceptor terminates application when receiving connection request using async_accept

From Dev

basic_ifstream<...>::read() doesn't read anything

From Dev

basic_ifstream<...>::read() doesn't read anything

From Dev

boost::asio::read() blocks forever

From Dev

Boost asio bind read handler

From Dev

boost::asio read/write trouble

From Dev

Boost asio asynchronous read and then write

Related Related

  1. 1

    boost::asio::read throws compilation error 'read_some' is not a member of 'boost::shared_ptr<boost::asio::ip::tcp::socket>'

  2. 2

    Move constructor is not called with boost::asio::ip::tcp::socket

  3. 3

    Move constructor is not called with boost::asio::ip::tcp::socket

  4. 4

    Writing sections of data to TCP socket with boost::asio::ip::tcp from streambuf

  5. 5

    HTTP GET request with a TCP socket doesn't return anything

  6. 6

    Boost::ASIO: using tcp::socket on multiple threads

  7. 7

    Boost::ASIO: using tcp::socket on multiple threads

  8. 8

    C++11 thread crash when access boost::asio::ip::tcp::socket

  9. 9

    Read Binary Data over TCP with Boost::Asio

  10. 10

    Boost Asio type to use for both unix-socket and tcp socket

  11. 11

    How do I send a POST with boost::asio::ip::tcp::iostream?

  12. 12

    Write from asio::ip::tcp::socket directly to std::string

  13. 13

    boost::asio::read prevents boost:asio::write from sending data to Java Socket

  14. 14

    boost::asio read n bytes from socket to streambuf

  15. 15

    Boost.Asio: socket::close not cancelling aysnc_read_some

  16. 16

    Can't set TCP source Port with boost asio

  17. 17

    Why doesn't file read output anything?

  18. 18

    Boost Asio incomplete write to socket

  19. 19

    Boost.Asio TCP moved-to socket destructor not enough to cleanly close?

  20. 20

    Android TCP socket doesn't receive data

  21. 21

    boost::asio::deadline_timer doesn't call handler

  22. 22

    boost::asio::deadline_timer doesn't call handler

  23. 23

    boost::asio::ip::tcp::acceptor terminates application when receiving connection request using async_accept

  24. 24

    basic_ifstream<...>::read() doesn't read anything

  25. 25

    basic_ifstream<...>::read() doesn't read anything

  26. 26

    boost::asio::read() blocks forever

  27. 27

    Boost asio bind read handler

  28. 28

    boost::asio read/write trouble

  29. 29

    Boost asio asynchronous read and then write

HotTag

Archive