Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Boost.Asio C++ Network Programming
Boost.Asio C++ Network Programming

Boost.Asio C++ Network Programming: Learn effective C++ network programming with Boost.Asio and become a proficient C++ network programmer

eBook
€17.98 €19.99
Paperback
€24.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with Print?

Product feature icon Instant access to your digital copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Redeem a companion digital copy on all Print orders
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Table of content icon View table of contents Preview book icon Preview Book

Boost.Asio C++ Network Programming

Chapter 1. Simplifying Your Network Programming in C++

There are several C++ compilers that we can choose from the Web. To make it easier for you to follow all the code in this book, I have chosen a compiler that will make the programming process simpler—definitely the easiest one. In this chapter, you will discover the following topics:

  • Setting up the MinGW compiler
  • Compiling in C++
  • Troubleshooting in GCC C++

Setting up the MinGW compiler and Text Editor

This is the hardest part—where we have to choose one compiler over the others. Even though I realize that every compiler has its own strength and weakness, I want to make it easier for you to go through all the code in this chapter. So, I suggest that you apply the same environment that we have, including the compiler that we use.

I am going to use GCC, the GNU Compiler Collection, because of its widely used open source. Since my environment includes Microsoft Windows as the operating system, I am going to use Minimalistic GCC for Windows (MinGW) as my C++ compiler. For those of you who have not heard about GCC, it is a C/C++ compiler that you can find in a Linux operating system and it is included in a Linux distribution as well. MinGW is a port of GCC to a Windows environment. Therefore, the entire code and examples in this book are applicable to any other GCC flavor.

Installing MinGW-w64

For your convenience, and since we use a 64-bit Windows operating system, we chose MinGW-w64 because it can be used for Windows 32-bits and 64-bits architecture. To install it, simply open your Internet browser and navigate to http://sourceforge.net/projects/mingw-w64/ to go to the download page, and click on the Download button. Wait for a moment until the mingw-w64-install.exe file is completely downloaded. Refer to the following screenshot to locate the Download button:

Installing MinGW-w64

Now, execute the installer file. You will be greeted by a Welcoming dialog box. Just press the Next button to go to the Setup Setting dialog box. In this dialog box, choose the latest GCC version (at the writing time this, it is 4.9.2), and the rest of the options are to be chosen, as follows:

Installing MinGW-w64

Click on the Next button to continue and go to the installation location option. Here, you can change the default installation location. I am going to change the installation location to C:\MinGW-w64 in order to make our next setting easier, but you can keep this default location if you want.

Installing MinGW-w64

Click on the Next button to go to the next step and wait for a moment until the files are downloaded and the installation process is complete.

Setting up the Path environment

Now you have the C++ compiler installed on your machine, but you can only access it from its installed directory. In order to access the compiler from any directory in your system, you have to set the PATH environment by performing the following steps:

  1. Run Command Prompt as an administrator by pressing the Windows + R key. Type cmd in the text box and, instead of pressing the Enter key, press Ctrl + Shift + Enter to run the command prompt in Administrator mode. The User Account Control dialog box will then appear. Choose YES to confirm that you intend to run Command Prompt in Administrator mode. If you do this correctly, you will get a title bar labeled Administrator: Command Prompt. If you do not get it, you might not have the administrator privilege. In this case, you have to contact the administrator of your computer.
  2. Type the following command in Command Prompt in Administrator mode:
    rundll32.exe sysdm.cpl,EditEnvironmentVariables
    
  3. Press the Enter key and the command prompt will immediately run the Environment Variables window. Afterwards, go to System variables, select the variable named Path, click on the Edit button to open the Edit System Variable dialog box, and then append the last Variable value parameter with the following string:
    ;C:\MinGW-w64\mingw64\bin

    (Otherwise, you will have to adjust the path of the installation directory if you use the default location the installation wizard is given in the previous step)

  4. Click on the OK button on the Edit System Variable dialog box, and click on the OK button again in the Environment Variables dialog box to save these changes.

It is time to try our Environment Variable setting. Open a new Command Prompt window, either in Administrator or non-Administrator mode, in any active directory except C:\MinGW-w64 and type the following command:

g++ --version

You have configured the proper settings if you see the output informing you the following:

g++ (x86_64-posix-seh-rev2, Built by MinGW-W64 project) 4.9.2

If you are showed a different version number, you might have another GCC compiler on your computer. To solve this problem, you can modify Environment Variable and remove all path environment settings associated with the other GCC compiler, for instance, C:\StrawberryPerl\c\bin.

However, if you do believe that you have followed all the steps correctly, but you still get an error message, as shown in the following snippet, you might have to restart your machine for your new system settings to be set:

'g++' is not recognized as an internal or external command, operable program or batch file.

Choosing and installing the Text Editor

Microsoft Windows has been equipped with Notepad, a simple text editor to create plain text files. You can use Notepad to create a C++ file, where the file must contain only plain text formatting. You can also turn to a heavy Integrated Development Environments (IDE) when you want to edit your code, but I prefer a simple, lightweight, and extensible programming plain-text editor, so I choose to use a text editor instead of IDE. Since I will need syntax highlighting when writing code to make it easier to read and understand, I pick Notepad++ as our text editor. You can choose your favorite text editor as long as you save the output file as plain text. Here is the sample of syntax highlighting in Notepad++:

Choosing and installing the Text Editor

If you decide to use Notepad++ as I did, you can go to http://notepad-plus-plus.org/ to grab the latest version of Notepad++. Find the Download menu on the main page and select the current version link. There, you will find a link to download the installer file. Use the Notepad++ Installer file instead of the package file to get the easiest way to set it up on your machine by following all the instructions on the installer wizard.

Choosing and installing the Text Editor

Using the GCC C++ compiler

Now that we have our development ready, we can write our first C++ program. To keep it clean, create a CPP folder in the C drive (C:\CPP) to store our sample code. You can have the same directory location on your system in order to follow all the steps more conveniently. Otherwise, you will have to make a little bit of modification if you decide to use a different directory location.

Compiling a C++ program

We won't create the Hello World! program for our first example code. It is boring in my opinion and, by now, you should already know how to code the Hello World! program. We are going to create a simple random number generator. You can use this program to play with your friends. They have to guess which number will be displayed by the program. If the answer is incorrect, you can cross out his/her face with a marker and continue playing until you are not able to recognize your friend's face anymore. Here is the code to create this generator:

/* rangen.cpp */
#include <cstdlib>
#include <iostream>
#include <ctime>
int main(void) {
  int guessNumber;
  std::cout << "Select number among 0 to 10:";
  std::cin >> guessNumber;
  if(guessNumber < 0 || guessNumber > 10) {
    return 1;
  }
  std::srand(std::time(0));
  int randomNumber = (std::rand() % (10 + 1));
  if(guessNumber == randomNumber) {
    std::cout << "Congratulation, " <<guessNumber<<" is your lucky number.\n";
  }
  else {
    std::cout << "Sorry, I'm thinking about number \n" << randomNumber;
  }
  return 0;
}

Type the code in your text editor and save it with the name of the file rangen.cpp in the C:\CPP location. Then, open Command Prompt and point the active directory to the C:\CPP location by typing the following command in Command Prompt:

cd C:\CPP

Next, type the following command in the console to compile the code:

g++ -Wall rangen.cpp -o rangen

The preceding command compiles the rangen.cpp file with an executable file named rangen.exe, which contains a bunch of machine code (the exe extension is automatically added to indicate that this file is an executable file in Microsoft Windows). The output file for the machine code is specified using the -o option. If you use this option, you have to specify the name of the output file as well; otherwise, the compiler will give you an error of a missing filename. If you omit both the -o option and the output's filename, the output is written to a default file called a.exe.

Tip

The existing executable file that has the same name as the compiled source file in the current directory will be overwritten.

I recommend that you use the -Wall option and make it a habit since this option will turn on all the most commonly used compiler warnings. If the option is disabled, GCC will not give you any warning. Because our Random Number Generator code is completely valid, GCC will not give out any warnings while it is compiled. This is why we depend on the compiler warnings to make sure that our code is valid and is compiled cleanly.

To run the program, type rangen in the console with the C:\CPP location as the active directory, and you will be showed a welcoming word: Select number among 0 to 10. Do what it instructs you to and choose a number between 0 to 10. Then, press Enter and the program will give out a number. Compare it with your own. If both the numbers are same, you will be congratulated. However, if your chosen number is different from the number the code generated, you will be informed the same. The output of the program will look as shown in the following screenshot:

Compiling a C++ program

Unfortunately, I never guessed the correct number in the three times that I tried. Indeed, it is not easy to guess which number the rand() function has generated, even if you use a new seed every time the number is generated. In order to minimize confusion, I am going to dissect the rangen.cpp code, as follows:

int guessNumber;
std::cout << "Select number among 0 to 10: ";
std::cin >> guessNumber;

I reserved a variable called guessNumber to store the integer number from the user and used the std::cin command to obtain the number that was input from the console.

if(guessNumber < 0 || guessNumber > 10) {
  return 1;
}

If the user gives an out-of-range number, notify the operating system that there is an error that has occurred in the program—I sent Error 1, but in practice, you can send any number—and let it take care of the error.

std::srand(std::time(0));
int randomNumber = (std::rand() % (10 + 1);

The std::srand function is used to initialize the seed, and in order to generate a different random number every time the std::rand() function is invoked, we use the std::time(0) function from the header ctime. To generate a range of random numbers, we use the modulo method that will generate a random number from 0 to (n-1) if you invoke a function like std::rand() % n. If you want to include the number n as well, simply add n with 1.

if(guessNumber == randomNumber) {
  std::cout << "Congratulation ,"<< guessNumber<<" is your lucky number.\n";
}
else {
  std::cout << "Sorry, I'm thinking about number " << randomNumber << "\n";
}

Here is the fun part, the program compares the user's guessed number with the generated random number. Whatever happens, the user will be informed of the result by the program. Let's take a look at the following code:

return 0;

A 0 return tells the operating system that the program has been terminated normally and that there is no need to worry about it. Let's take a look at the following code:

#include <cstdlib>
#include <iostream>
#include <ctime>

Do not forget to include the first three headers in the preceding code since they contain the function that we used in this program, such as the time() function is defined in the <ctime> header, the srand() function and the rand() function are defined in the <cstdlib> header, and the cout() and cin() functions are defined in the <iostream> header.

If you find that it is hard to guess a number that the program has generated, this is because we use the current time as the random generator seed, and the consequence of this is that the generated number will always be different in every invocation of the program. Here is the screenshot of when I could guess the generated random number correctly after about six to seven attempts (for all the program invocations, we guessed the number incorrectly except for the last attempt):

Compiling a C++ program

Compiling multiple source files

Sometimes, we have to modify our code when it has bugs or errors. If we just make a single file that contains all the lines of code, we will be confused when we want to modify the source or it will be hard for us to understand the flow of the program. To solve the problem, we can split up our code into multiple files where every file contains only two to three functions so that it is easy to understand and maintain them.

We have already been able to generate random numbers, so now, let's take a look at the password generator program. We are going to use it to try compiling multiple source files. I will create three files to demonstrate how to compile multiple source files, which are pwgen_fn.h, pwgen_fn.cpp, and passgen.cpp. We will start from the pwgen_fn.h file whose code is as follows:

/* pwgen_fn.h */
#include <string>
#include <cstdlib>
#include <ctime>
class PasswordGenerator {
  public:
    std::string Generate(int);
};

The preceding code is used to declare the class name. In this example, the class name is PasswordGenerator, and what it will do in this case is generate the password while the implementation is stored in the .cpp file. The following is a listing of the pwgen_fn.cpp file, which contains the implementation of the Generate() function:

/* pwgen_fn.cpp */
#include "pwgen_fn.h"
std::string PasswordGenerator::Generate(int passwordLength) {
  int randomNumber;
  std::string password;
  std::srand(std::time(0));
  for(int i=0; i < passwordLength; i++) {
    randomNumber = std::rand() % 94 + 33;
    password += (char) randomNumber;
  }
  return password;
}

The main entry file, passgen.cpp, contains a program that uses the PasswordGenerator class:

/* passgen.cpp */
#include <iostream>
#include "pwgen_fn.h"
int main(void) {
  int passLen;
  std::cout << "Define password length: ";
  std::cin >> passLen;
  PasswordGenerator pg;
  std::string password = pg.Generate(passLen);
  std::cout << "Your password: "<< password << "\n";
  return 0;
}

From the preceding three source files, we will produce a single executable file. To do so, go to Command Prompt and type the following command in it:

g++ -Wall passgen.cpp pwgen_fn.cpp -o passgen

I did not get any warning or error, so even you should not. The preceding command compiles the passgen.cpp and pwgen_fn.cpp files and then links them together to a single executable file named passgen.exe. The pwgen_fn.h file, since it is the header file that has same name as the source file, does not need to state the same in the command.

Here is what you will get if you run the program by typing the passgen command in the console window; you will get a different password every time the program is run:

Compiling multiple source files

Now, it is time for us to dissect the preceding source code. We will start from the pwgen_fn.h file, which only contains the function declaration, as follows:

std::string Generate(int);

As you can see from the declaration, the Generate() function will have a parameter with the int type and will return the std::string function. We do not define a name for the parameter in the header file since it will be matched with the source file automatically.

Open the pwgen_fn.cpp file, to see the following statement:

std::string PasswordGenerator::Generate(int passwordLength)

Here, we can specify the parameter name, which is passwordLength. In this case, we can have two or more functions with the same name as long as they are in different classes. Let's take a look at the following code:

int randomNumber;
std::string password;

I reserved the variable named randomNumber to store random numbers generated by the rand() function and the password parameter to store the ASCII converted from the random number. Let's take a look at the following code:

std::srand(std::time(0));

The seed random srand() function is the same as what we used in our previous code to generate a random seed. We used it in order to produce a different number every time the rand() function is invoked. Let's take a look at the following code:

for(int i=0; i < passwordLength; i++) {
  randomNumber = std::rand() % 94 + 33;
  password += (char) randomNumber;
}
return password;

The for iteration depends on the passwordLength parameter that the user has defined. With the random number generator statement std::rand() % 94 + 33, we can generate the number that represents the ASCII printable character based on its code from 33 to 126. For more detailed information about the ASCII code table, you can go to http://en.wikipedia.org/wiki/ASCII. Let's take a look at the following code:

#include "pwgen_fn.h"

The #include header's single line will call all headers included in the pwgen_fn.h file, so we do not need to declare the included header in this source file as follows:

#include <string>
#include <cstdlib>
#include <ctime>

Now, we move to our main entry code, which is stored in the passgen.cpp file:

int passLen;
std::cout << "Define password length: ";
std::cin >> passLen;

First, the user decides how long a password he/she wants to have, and the program stores it in the passLen variable:

PasswordGenerator pg;
std::string password = pg.Generate(passLen);
std::cout << "Your password: "<< password << "\n";

Then, the program instantiates the PasswordGenerator class and invokes the Generate() function to produce a password with the length that the user has defined before.

If you look at the passgen.cpp file again, you will find that there is a difference between the two forms of the include statement #include <iostream> (with angle brackets) and #include "pwgen_fn.h" (with quotation marks). By using angle brackets in the #include header statement, the compiler will look for the system header file directories, but does not look inside the current directory by default. With the quotation marks in the #include header statement, the compiler will search for the header files in the current directory before looking in the system header file directories.

Compiling and linking a program separately

We can split up a large program into a set of source files and compile them separately. Suppose we have many tiny files and we just want to edit a single line in one of the files, it will be very time consuming if we compile all the files while we just need to modify a single file.

By using the -c option, we can compile the individual source code to produce an object file that has the .o extension. In this first stage, a file is compiled without creating an executable file. Then, in the second stage, the object files are linked together by a separate program called the linker. The linker combines all the object files together to create a single executable file. Using the previous passgen.cpp, pwgen_fn.cpp, and pwgen_fn.h source files, we will try to create two object files and then link them together to produce a single executable file. Use the following two commands to do the same:

g++ -Wall -c passgen.cpp pwgen_fn.cpp
g++ -Wall passgen.o pwgen_fn.o -o passgen

The first command, using the -c option, will create two object files that have the same name as the source file name, but with different extensions. The second command will link them together and produce the output executable file that has the name stated after the -o option, which is the passgen.exe file.

In case you need to edit the passgen.cpp file without touching the two other files, you just require to compile the passgen.cpp file, as follows:

g++ -Wall -c passgen.cpp

Then, you need to run the linking command like the preceding second command.

Detecting a warning in the C++ program

As we discussed previously, a compiler warning is an essential aid to be sure of the code's validity. Now, we will try to find the error from the code that we created. Here is a C++ code that contains an uninitialized variable, which will give us an unpredictable result:

/* warning.cpp */
#include <iostream>
#include <string>
int main (void) {
  std::string name;
  int age;
  std::cout << "Hi " << name << ", your age is " << age << "\n";
}

Then, we will run the following command to compile the preceding warning.cpp code:

g++ -Wall -c warning.cpp

Sometimes, we are unable to detect this error since it is not obvious at the first sight. However, by enabling the -Wall option, we can prevent the error because if we compile the preceding code with the warning option enabled, the compiler will produce a warning message, as shown in the following code:

warning.cpp: In function 'int main()':
warning.cpp:7:52: warning: 'age' may be used uninitialized in this function [-Wmaybe-uninitialized]
std::cout << "Hi " << name << ", your age is " << age << "\n";]

The warning message says that the age variable is not initialized in the warning.cpp file on the line 7, column 52. The messages produced by GCC always have the file:line-number:column-number:error-type:message form. The error type distinguishes between the error messages, which prevent the successful compilation, and warning messages, which indicate the possible problems (but do not stop the program from compiling).

Clearly, it is very dangerous to develop a program without checking for compiler warnings. If there are any functions that are not used correctly, they can cause the program to crash or produce incorrect results. After turning the compiler warning option on, the -Wall option catches many of the common errors that occur in C++ programming.

Knowing other important options in the GCC C++ compiler

GCC supports ISO C++ 1998, C++ 2003, and also C++ 2011 standard in version 4.9.2. Selecting this standard in GCC is done using one of these options: -ansi, -std=c++98, -std=c++03, or –std=c++11. Let's look at the following code and give it the name hash.cpp:

/* hash.cpp */
#include <iostream>
#include <functional>
#include <string>
int main(void) {
  std::string plainText = "";
  std::cout << "Input string and hit Enter if ready: ";
  std::cin >> plainText;
  std::hash<std::string> hashFunc;
  size_t hashText = hashFunc(plainText);
  std::cout << "Hashing: " << hashText << "\n";
  return 0;
}

If you compile and run the program, it will give you a hash number for every plain text user input. However, it is little tricky to compile the preceding code. We have to define which ISO standard we want to use. Let's take a look at the following five compilation commands and try them one by one in our Command Prompt window:

g++ -Wall hash.cpp -o hash
g++ -Wall -ansi hash.cpp -o hash
g++ -Wall -std=c++98 hash.cpp -o hash
g++ -Wall -std=c++03 hash.cpp -o hash
g++ -Wall -std=c++11 hash.cpp -o hash

When we run the first four preceding compilation commands, we should get the following error message:

hash.cpp: In function 'int main()':
hash.cpp:10:2: error: 'hash' is not a member of 'std'
  std::hash<std::string> hashFunc;
hash.cpp:10:23: error: expected primary-expression before '>' token
  std::hash<std::string> hashFunc;
hash.cpp:10:25: error: 'hashFunc' was not declared in this scope
  std::hash<std::string> hashFunc;

It says that there is no hash in the std class. Actually, this is not true as a hash has been defined in the header <string> since C++ 2011. To solve this problem, we can run the last preceding compilation command, and if it does not throw an error anymore, then we can run the program by typing hash in the console window.

Knowing other important options in the GCC C++ compiler

As you can see in the preceding screenshot, I invoked the program twice and gave Packt and packt as the input. Although I just changed a character, the entire hash changed dramatically. This is why hashing is used to detect any change in data or a file if they are transferred, just to make sure the data is not altered.

For more information about ISO C++11 features available in GCC, go to http://gcc.gnu.org/projects/cxx0x.html. To obtain all the diagnostics required by the standard, you should also specify the -pedantic option (or the -pedantic-errors option if you want to handle warnings as errors).

Note

The -ansi option alone does not cause non-ISO programs to be rejected gratuitously. For that, the -pedantic option or the -pedantic-errors option is required in addition with the -ansi option.

Troubleshooting in the GCC C++ compiler

GCC provides several help and diagnostic options to assist in troubleshooting problems with the compilation process. The options that you can use to ease your troubleshooting process are explained in the upcoming sections.

Help for command-line options

Use the help options to get a summary of the top-level GCC command-line options. The command for this is as follows:

g++ --help

To display a complete list of the options for GCC and its associated programs, such as the GNU Linker and GNU Assembler, use the preceding help option with the verbose (-v) option:

g++ -v --help

The complete list of options produced by the preceding command is extremely long—you may wish to go through it using the more command or redirect the output to a file for reference, as follows:

g++ -v --help 2>&1 | more

Version numbers

You can find the version number of your installed GCC installation using the version option, as shown in the following command:

g++ --version

In my system, if I run the preceding command, I will get an output like this:

g++ (x86_64-posix-seh-rev2, Built by MinGW-W64 project) 4.9.2

This depends on your setting that you adjust at the installation process.

The version number is important when investigating compilation problems, since older versions of GCC may be missing some features that a program uses. The version number has the major-version.minor-version or major-version.minor-version.micro-version form, where the additional third "micro" version number (as shown in the preceding command) is used for subsequent bug fix releases in a release series.

The verbose compilation

The -v option can also be used to display detailed information about the exact sequence of commands that are used to compile and link a program. Here is an example that shows you the verbose compilation of the hello.cpp program:

g++ -v -Wall rangen.cpp

After this, you will get something like this in the console:

Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=C:/mingw-w64/bin/../libexec/gcc/x86_64-w64-mingw32/4.9.2/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../../../src/gcc-4.9.2/configure –
...Thread model: posix
gcc version 4.9.2 (x86_64-posix-seh-rev2, Built by MinGW-W64 project)
...

The output produced by the -v option can be useful whenever there is a problem with the compilation process itself. It displays the full directory paths used to search for header files and libraries, the predefined preprocessor symbols, and the object files and libraries used for linking.

Summary

We successfully prepared the C++ compiler and you learned how to compile the source code file you created using the compiler. Do not forget to use the -Wall (Warning All) option every time you compile the source code because it is important to avoid a warning and subtle error. Also, it is important to use the -ansi and -pedantic options so that your source code is able to be compiled in any compiler, as it will check the ANSI standard and reject non-ISO programs.

Now, we can go to the next chapter to learn the networking concept so that you can understand network architecture in order to ease your network application programming process.

Left arrow icon Right arrow icon

Description

Boost.Asio is a C++ library used for network programming operations. Organizations use Boost because of its productivity. Use of these high-quality libraries speed up initial development, result in fewer bugs, reduce reinvention-of-the-wheel, and cut long-term maintenance costs. Using Boost libraries gives an organization a head start in adopting new technologies. This book will teach you C++ Network programming using synchronous and asynchronous operations in Boost.Asio with minimum code, along with the fundamentals of Boost, server-client applications, debugging, and more. You will begin by preparing and setting up the required tools to simplify your network programming in C++ with Boost.Asio. Then you will learn about the basic concepts in networking such as IP addressing, TCP/IP protocols, and LAN with its topologies. This will be followed by an overview of the Boost libraries and their usage. Next you will get to know more about Boost.Asio and its concepts related to network programming. We will then go on to create a client-server application, helping you to understand the networking concepts. Moving on, you will discover how to use all the functions inside the Boost.Asio C++ libraries. Lastly, you will understand how to debug the code if there are errors found and will run the code successfully.

Who is this book for?

This book is for C++ Network programmers with basic knowledge of network programming, but no knowledge of how to use Boost.Asio for network programming.

What you will learn

  • Prepare the tools to simplify network programming in C++ using Boost.Asio
  • Explore the networking concepts of IP addressing, TCP/IP ports and protocols, and LAN topologies
  • Get acquainted with the usage of the Boost libraries
  • Get to know more about the content of Boost.Asio network programming and Asynchronous programming
  • Establish communication between client and server by creating clientserver application
  • Understand the various functions inside Boost.Asio C++ libraries to delve into network programming
  • Discover how to debug and run the code successfully
Estimated delivery fee Deliver to Sweden

Premium delivery 7 - 10 business days

€17.95
(Includes tracking information)

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Sep 16, 2015
Length: 200 pages
Edition : 1st
Language : English
ISBN-13 : 9781785283079
Category :
Languages :
Tools :

What do you get with Print?

Product feature icon Instant access to your digital copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Redeem a companion digital copy on all Print orders
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Estimated delivery fee Deliver to Sweden

Premium delivery 7 - 10 business days

€17.95
(Includes tracking information)

Product Details

Publication date : Sep 16, 2015
Length: 200 pages
Edition : 1st
Language : English
ISBN-13 : 9781785283079
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 108.97
Boost.Asio C++ Network Programming Cookbook
€41.99
Learning Boost C++
€41.99
Boost.Asio C++ Network Programming
€24.99
Total 108.97 Stars icon

Table of Contents

8 Chapters
1. Simplifying Your Network Programming in C++ Chevron down icon Chevron up icon
2. Understanding the Networking Concepts Chevron down icon Chevron up icon
3. Introducing the Boost C++ Libraries Chevron down icon Chevron up icon
4. Getting Started with Boost.Asio Chevron down icon Chevron up icon
5. Delving into the Boost.Asio Library Chevron down icon Chevron up icon
6. Creating a Client-server Application Chevron down icon Chevron up icon
7. Debugging the Code and Solving the Error Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.4
(11 Ratings)
5 star 27.3%
4 star 27.3%
3 star 18.2%
2 star 9.1%
1 star 18.2%
Filter icon Filter
Top Reviews

Filter reviews by




ELIAS POLITAKIS Oct 04, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
By far one of the best books I have read regarding a specific technological topic. The book is extremely well structured, starting from setting up the compiler, introducing networking principles and structures, moving to building boost libraries and then getting into ASIO with good examples of asynchronous client and server examples. It will give you everything you need to know about ASIO for getting started in less than 24 hours. Highly recommended!!
Amazon Verified review Amazon
Maxim Rozhkov Dec 17, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
The authors clearly explained some important issues, related with network programming by Boost.ASIO, in the book. Particularly, they gave a lot of information about programming tools (MinGW), the OSI model, the concept of multi-threading programming in the Boost and they described an example of a Telnet application (client and server sides). The most impressing part of the book for me is considered the model of multi-threading in the Boost libraries. I found that all insertions about C++ 11 were amazingly useful: smart-pointers, move-semantics, etc. The book will be interesting and useful for everyone who is interested in network programming and development of C++. No one minute that was spent on reading the book was not lost in vain.
Amazon Verified review Amazon
luncliff Apr 23, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Reference codes are easy
Amazon Verified review Amazon
Hossein Oct 22, 2024
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
The book seems really useful and gives reader enough information to understand how to use boost.asio library properly. The code examples are long and there are too much unnecessary and irrelevant details in them. I tried to rewrite the example codes to only contain the most simple meanings and used as few components as possible, and the boost.asio details became a lot more visible. Using other libraries like shared_ptr, random and thread while we have them in the standard library is not a wise thing to do in a simple book like this that has only one purpose, to teach boost.asio not any other libraries. Critics aside, the book is worth the time needed to read it. Recommend it.
Subscriber review Packt
Andrea Oct 27, 2015
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
Already had the 1st edition and since I had found it useful, I decided to upgrade to the 2nd edition.It was a good surprise to find the content is now more polished and a bit better structured.Overall, the book is valuable if you need to quickly start with boost asio and more generally if you need to get a first grasp at the concepts of asynchronous network programming.The foundation of the paradigms is well explained and gives you a jump start in using the library, which I found very useful as I was able to apply the concepts and the techniques in a real application in very short time.The examples are very practical and give you a good starting point upon which you can then improve and customize as you prefer.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is the digital copy I get with my Print order? Chevron down icon Chevron up icon

When you buy any Print edition of our Books, you can redeem (for free) the eBook edition of the Print Book you’ve purchased. This gives you instant access to your book when you make an order via PDF, EPUB or our online Reader experience.

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela