Boost - Asio C++ Network Programming - Second Edition - Sample Chapter
Boost - Asio C++ Network Programming - Second Edition - Sample Chapter
Second Edition
Boost.Asio is a C++ library used for network programming
operations.
Organizations use Boost because of its productivity. Using
of these high-quality libraries speeds up initial development,
results in fewer bugs, stops you from reinventing the wheel,
and cuts long-term maintenance costs. Using the Boost
libraries gives an organization a head start in adopting new
technologies.
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 topologies. This will be followed by an overview
of the Boost libraries and their usage. Moving on, you will
discover how to use all the functions inside the Boost.Asio
C++ libraries. Lastly, you will understand how to debug code
if there are errors found and how to run code successfully.
pl
C o m m u n i t y
E x p e r i e n c e
D i s t i l l e d
Second Edition
Wisnu Anggoro
John Torjo
P U B L I S H I N G
Sa
m
Boost.Asio C++
Network Programming
$ 29.99 US
19.99 UK
Boost.Asio C++
Network Programming
ee
John Torjo is a renown C++ expert. He has been programming for over 15 years,
most of which were spent doing C++. Sometimes, he also codes C# or Java. He's
also enjoyed writing articles about programming in C++ Users Journal (currently,
Dr. Dobbs) and other magazines. In his spare time, he likes playing poker and
driving fast cars. One of his freelance projects lets him combine two of his passions,
programming and poker. You can reach him at [email protected].
Preface
Network applications were not very easy to develop about two decades ago.
But thanks to Boost.Asio, which has provided us with the network programming
function as well as the asynchronous operations functionality to program a network
application, we can now develop them easily. Since data transmission over a
network can take a long time, which means acknowledgments and errors may
not be available as fast as the functions that send or receive data can execute, the
asynchronous operations functionality is really required in network application
programming. In this book, you will learn the basics of networking and also how
to develop a network application using the Boost.Asio libraries.
Preface
Chapter 5, Delving into the Boost.Asio Library, walks us through how to serialize an
I/O service's work in order to ensure that the order of work completely matches
the order we have designed. It also covers how to handle errors and exceptions and
create time delays in network programming.
Chapter 6, Creating a Client-server Application, discusses developing a server that is
able to send and receive data traffic from a client and also how to create a client-side
program to receive data traffic.
Chapter 7, Debugging the Code and Solving the Error, covers the debugging process to
trace the errors that may be produced by an unexpected result, such as getting crash
in the middle of a program execution. After reading this chapter, you will be able to
solve various errors by debugging the code.
Compiling in C++
[1]
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-w64install.exe file is completely downloaded. Refer to the following screenshot
to locate the Download button:
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:
[2]
Chapter 1
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.
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.
[3]
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.
[4]
Chapter 1
If you decide to use Notepad++ as I did, you can go to http://notepad-plusplus.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.
[5]
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
[6]
Chapter 1
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.
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:
[7]
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 programI sent Error 1, but in practice, you can send
any numberand 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;
[8]
Chapter 1
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):
[9]
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;
}
[ 10 ]
Chapter 1
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:
[ 11 ]
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;
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"
[ 12 ]
Chapter 1
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.
[ 13 ]
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.
[ 14 ]
Chapter 1
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
[ 15 ]
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.
[ 16 ]
Chapter 1
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).
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.
[ 17 ]
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
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.minorversion.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.
[ 18 ]
Chapter 1
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-w64mingw32/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.
[ 19 ]
www.PacktPub.com
Stay Connected: