dimitris93 dimitris93 - 1 year ago 144
C++ Question

boost::asio read response

I am trying to connect to an IRC server (twitch.tv). The connection is successful but I don't know how to read the response from the server properly. More specifically I have trouble in this line:

boost::asio::read(s, boost::asio::buffer(reply, MAX_LENGTH));

If I use
(1024), which is a number larger than the size of the response message then the program doesn't seem to terminate. If I use a number like 64, I can read only 64 characters of the message. Can I somehow read until
or something ? How can I do this properly ?

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

using namespace std;

int main(int argc, char *argv[])
char HOSTNAME[] = "irc.chat.twitch.tv";
char PORT[] = "6667";
char PASS[] = "PASS oauth:123\r\n";
char NICK[] = "NICK 123\r\n";
char USER[] = "USER 123\r\n";
const size_t MAX_LENGTH = 1024;

boost::asio::io_service io_service;
boost::asio::ip::tcp::socket s(io_service);
boost::asio::ip::tcp::resolver resolver(io_service);
boost::asio::connect(s, resolver.resolve({HOSTNAME, PORT}));
cout << "connected\n";

boost::asio::write(s, boost::asio::buffer(PASS, strlen(PASS)));
boost::asio::write(s, boost::asio::buffer(NICK, strlen(NICK)));
boost::asio::write(s, boost::asio::buffer(USER, strlen(USER)));
cout << "sent 3 messages\n";

char reply[MAX_LENGTH];

size_t reply_length = boost::asio::read(s,
boost::asio::buffer(reply, MAX_LENGTH));
// execution never reaches here
std::cout << "Reply is: ";
std::cout.write(reply, reply_length);
std::cout << "\n";
catch (std::exception &e)
std::cerr << "Exception: " << e.what() << "\n";

return 0;

Answer Source

Read tries to read until the buffer is full before returning. If you want the function to return after having read anything at all use read_some.

s.read_some(boost::asio::buffer(reply, MAX_LENGTH));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download