einstein einstein - 1 year ago 74
C++ Question

Cannot cout string when boost's io service is running

I have some simple code:

#include <iostream>
#include <string>
int main() {
boost::asio::io_service ioservice;
TCPServer server(ioservice);
std::cout << "hello world";

I cout the hello world string above. Though the string is not being outputted on my terminal when I call
. When I remove the last call expression an output can be seen. What bugs me is that I cout before the call and nothing is being outputted. It appears also that cout is not available even on handlers defined on my TCP server. So how do people log to stdout when using Boost Asio?

Answer Source

You didn't flush the stream before entering .run(), so your information is still sitting in the buffer.

Use std::cout << std::flush (or std::cout << std::endl to include a newline too).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download