Newbie Boost Question

Hey guys,

So I have a pretty simple boost server running that:

1) Async_accepts connections
2) Writes to the connection
3) Kills the connection
4) Keeps running

but I can't get any std::cout to print to the console while the server is running.

The main file looks like this:

1
2
3
4
5
6
7
8
int main(int argc, char* argv[])
{

	std::cout << "hi";
	boost::asio::io_context io_context;
	Server cyrServer(io_context, 4500); // non-blocking "async_accept" 
	io_context.run();
}


and that std::cout << "hi"; is never getting output to the console.

I can post more of the code for you, but it pretty much just follows the TCP boost async server tutorial here: https://www.boost.org/doc/libs/1_70_0/doc/html/boost_asio/tutorial/tutdaytime3.html
Try adding something at the end, after io_context.run(), which pauses the execution.

This might do:

1
2
3
int i{0};
std::cout << "Hit any key to continue...."
std::cin >> i;


It may be nothing more than the console window is appearing, the message displays, then the console closes, all so quickly you never see it.

Also, it is not quite right to say this is a boost server. It is a boost::asio server.

Each library in boost is "it's own thing", and asio may well become part of the std library soon. It won't be boost then.
have you tried just printing?
Hi Niccolo,

Thanks for your reply. I'm sure that's not the problem, as the console window doesn't close.

The server starts and continuously waits for a connection until the command-line kill signal is given, and even after that the console window remains open. The problem is that it doesn't allow for any console output while the server is in that awaiting-connection state.

More likely, it has to do with io_context.run() holding the thread hostage with busywork to keep the server alive, and that is somehow interfering with std::couts ability to print to standard out. This is just conjecture though and I'd need someone to verify.

This problem is completely separate from a client connection. I'm not trying to send or receive data over the server. Just output to the console while the server is running. For instance, when the server boots, I might want to output "Server has booted", but nothing appears.
Could it be that you just need a flush? A newline should do the trick. Or std::endl if you want an explicit flush as well as the newline.

 
std::cout << "hi" << std::endl;
Last edited on
Ok, then your cout is simply not flushed.

Send an "endl" at the end, like this:

cout << "Hello" << endl;

That ends the line AND flushes the stream immediately. The text will appear, but THEN you'll see the hang continue.

Two separate issues.

Now, as to io_context run....


This is a run loop. It is supposed to "hang" - it services the asio connection system. Servers are simply supposed to continue operating until shut down.

The issue is that asio is far more complex than this simple example will logically handle. It may actually work, don't get me wrong (though I feel certain it isn't enough to actually receive anything), but this is way too simple a configuration of code to actually work.

Asio, put simply, requires threads - and it should manage threads intelligently (it has support to handle thousands of connections on a few threads launched in acknowledgement of the limited number of cores on a particular processor).

You'll simply need to run through the tutorial examples for building Asio servers to continue.

Last edited on
Well,

That's embarassing. I guess it just needed a flush. Was wondering why I couldn't find more articles on something like this, as if it were a common problem, it should be easy to find.

I searched "io_context blocking output" "boost asio std::cout" and all sorts of other things but couldn't find anything.

Wow....

Thanks.
@dutch and I often end up thinking the same thing at the same time...:)
Topic archived. No new replies allowed.