Networking with C++

Networking with C++

Given your newness to C++, I would not recommend building directly on the sockets APIs unless you can find no suitable library to use. Boost.Asio will give you a huge head start and expose you to the higher-level abstractions used in network programming.

Its easy when starting out building a sockets-based system to get something that sort of works and then spend weeks debugging corner cases that only happen under real-world timing and load conditions. Using boost::asio correctly is hardly a cakewalk even if it shields developers from the complexities of raw socket handling.

If the goal is to learn how to use raw sockets (or some other transport mechanism such as RPC) correctly, then by all means roll your own using online samples and docs to understand the individual BSD or Winsock APIs – if the goal is to solve a business problem as quickly as possible with high quality code on both business and networking infrastructure side, then use a good networking library. In this case your question does indicate a wish to learn so using a library may not be the best way to achieve your stated goal.

You should check out these guides about:

And Boost.Asio Look here for a question on documentation related to Boost.Asio

Networking with C++

Im sure a bunch of C++ people who despise the C way of doing things will hate me for this, but the classical approach is to use the Berkeley socket APIs (so-called because they have their origins in BSD). If youre targeting Windows, the largely-source-compatible, inspired-by-Unix APIs are called Winsock. Id say do a web search for socket tutorial and you will probably get some useful information. With a little care and maybe an #ifdef or two, its not so hard to create code which works on Linux, BSD, Mac (which provide BSD sockets) and Windows (with Winsock) using whats common between these two interfaces.

For the more C++ inclined, less C-style people, or those who dont like to code against OS APIs, Im sure there are C++ libraries out there that provide wrappers and abstractions for these same concepts. Since Boost seems to be pretty popular, Id say see what they have.

Leave a Reply

Your email address will not be published.