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

Synchronous I/O in server applications

Servers, like clients, are divided into two cases; they match scenarios one and two from the previous section. Again, both scenarios use a make-request-read-answer strategy.

Synchronous I/O in server applications

The first scenario is the synchronous server we've implemented in the previous chapter. Reading a full request is not easy when you're synchronous, since you want to avoid blocking (you always read as much as you can):

void read_request() {
    if ( sock_.available())
        already_read_ += sock_.read_some(
            buffer(buff_ + already_read_, max_msg - already_read_));
}

Once a message has been fully read, just process it and answer the client:

void process_request() ...

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