Book Image

Boost.Asio C++ Network Programming

By : Wisnu Anggoro
Book Image

Boost.Asio C++ Network Programming

By: Wisnu Anggoro

Overview of this book

Table of Contents (15 chapters)
Boost.Asio C++ Network Programming Second Edition
Credits
About the Authors
Acknowledgements
About the Reviewers
www.PacktPub.com
Preface
Index

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:

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 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:

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.