O'Reilly logo

Boost.Asio C++ Network Programming by John Torjo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Asynchronous I/O in server applications

Here are the ubiquitous two cases, scenario one (pull) and scenario 2 (push):

Asynchronous I/O in server applications

The first scenario is the asynchronous server I implemented in Chapter 4, Client and Server again. At the end of each asynchronous operation, I start another asynchronous operation, so that service.run() doesn't end.

Here's the skeleton code, which is trimmed down. The following are all members in talk_to_client class:

void start() { ... do_read(); // first, we wait for client to login } void on_read(const error_code & err, size_t bytes) { std::string msg(read_buffer_, bytes); if ( msg.find("login ") == 0) on_login(msg); else if ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required