CRC Checksums and Time-outs sample code won't compile or run

Volume II - Advanced Libraries

CRC Checksums and Time-outs sample code won't compile or run

Postby abl » Thu Feb 28, 2013 9:50 pm

The sample code from book 2, chapter 10, section 6, CRC Checksums and Time-outs fails to compile or even run under CentOS release 6.3 (Final) -- CentOS is essentially RedHat -- with GNU g++ 4.4.6 20120305 (Red Hat 4.4.6-4) and with BOOST_VERSION 105200 and BOOST_LIB_VERSION "1_52" from Boost version.hpp:



Code: Select all
[user@host current_pathname]$ g++ -g -I. -I/usr/local/include -I/usr/local/include/boost -L/usr/local/lib -lboost_locale -lboost_thread Main.cpp

In file included from Main.cpp:15:

Client.hpp: In member function ?void Client::HandleReadHeader(Client::MessagePtr, const boost::system::error_code&)?:

Client.hpp:211: error: ?Beep? was not declared in this scope

In file included from Main.cpp:15:

Client.hpp: In member function ?void Client::StartSendHeartbeat()?:

Client.hpp:348: error: ?Beep? was not declared in this scope

In file included from Main.cpp:16:

Server.hpp: In member function ?void ASyncConnectionMt::HandleReadBody(ASyncConnectionMt::MessagePtr, const boost::system::error_code&)?:

Server.hpp:192: error: no matching function for call to ?transform(__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, <unresolved overloaded function type>)?

[user@host current_pathname]$


Beep() is a Windows function, but I'm running on Linux. I know how to make noise on Linux with echo, but I don't need that to see the program work. I comment out the lines with Beep() in them:

Code: Select all
[user@host current_pathname]$ grep -n Beep *.[ch]pp

Client.hpp:211:         Beep(1400, 200);

Client.hpp:348:      Beep(700, 200);

[user@host current_pathname]$
and recompile:

Code: Select all
[user@host current_pathname]$ g++ -g -I. -I/usr/local/include -I/usr/local/include/boost -L/usr/local/lib -lboost_locale -lboost_thread Main.cpp

In file included from Main.cpp:16:

Server.hpp: In member function ?void ASyncConnectionMt::HandleReadBody(ASyncConnectionMt::MessagePtr, const boost::system::error_code&)?:

Server.hpp:192: error: no matching function for call to ?transform(__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, <unresolved overloaded function type>)?

[user@host current_pathname]$


The compiler can't find toupper. That is not a problem because I can live without a translation to upper case as evidence that a message reached the server. I comment out that line, and the compiler runs error free and links a binary.



I run the binary and type 2 to get the server running:

Code: Select all
[user@host current_pathname]$ a.out

TCP networking example.



Debugging commands after connecting:

- ClientHeartbeatOn

- ServerHeartbeatOn

- ClientHeartbeatOff

- ServerHeartbeatOff

- ClientSendCrcError

- ServerSendCrcError



1. Client

2. Server

Choice: 2

(thread id: 7f8a09f3e720) Listening to TCP socket on port 60000...


That is excellent! Encouraged, I start the client and watch it fail:

Code: Select all
[user@host current_pathname]$ a.out

TCP networking example.



Debugging commands after connecting:

- ClientHeartbeatOn

- ServerHeartbeatOn

- ClientHeartbeatOff

- ServerHeartbeatOff

- ClientSendCrcError

- ServerSendCrcError



1. Client

2. Server

Choice: 1

Enter the host to connect to (empty=localhost):



Debugging commands:

- ClientHeartbeatOff

- ClientHeartbeatOn

- ServerHeartbeatOff

- ServerHeartbeatOn

- ClientSendCrcError

- ServerSendCrcError



Connecting to 127.0.0.1:60000...

Connected to 127.0.0.1:60000



Enter text to send:

1234

terminate called after throwing an instance of 'std::length_error'

  what():  basic_string::_S_create

  Aborted

[user@host current_pathname]$


Longer text or one of the commands elicits a different error:

Code: Select all
[user@host current_pathname]$ a.out

TCP networking example.



Debugging commands after connecting:

- ClientHeartbeatOn

- ServerHeartbeatOn

- ClientHeartbeatOff

- ServerHeartbeatOff

- ClientSendCrcError

- ServerSendCrcError



1. Client

2. Server

Choice: 1

Enter the host to connect to (empty=localhost):



Debugging commands:

- ClientHeartbeatOff

- ClientHeartbeatOn

- ServerHeartbeatOff

- ServerHeartbeatOn

- ClientSendCrcError

- ServerSendCrcError



Connecting to 127.0.0.1:60000...

Connected to 127.0.0.1:60000



Enter text to send:

ServerHeartbeatOff

terminate called after throwing an instance of 'std::bad_alloc'

  what():  std::bad_alloc

  Aborted

[user@host current_pathname]$


Please post a working copy of that sample program.
abl
 
Posts: 6
Joined: Thu Feb 28, 2013 3:42 am

Postby Cuchulainn » Mon Mar 04, 2013 3:51 pm

We are not familar with, and do not support CentOS. So my question does this boost library support the OS or is it an application error from the book?



Can you run it under Windows as well? If it is giving problems we will fix it.
User avatar
Cuchulainn
 
Posts: 668
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Postby abl » Mon Mar 04, 2013 9:59 pm

> We are not familar with, and do not support CentOS.




As I wrote in my original post, CentOS is essentially RedHat -- with GNU g++ 4.4.6 20120305 (Red Hat 4.4.6-4).



CentOS is built from all the RedHat sources that are not proprietary to RedHat. Thus, CentOS provides the stability of RedHat without RedHat's fees.



If you support RedHat Linux, you already support CentOS.



Do you support RedHat Linux?



Do you support any Linux systems, or does your example code run only on Windows?



So my question does this boost library support the OS or is it an application error from the book?




Boost.org's page http://www.boost.org/users/history/version_1_52_0.html lists only compilers under Linux not distributions of Linux. The answer to your question is yes my OS is supported under that version of Boost.



In that list, GCC 4.4.3 appears; because my compiler is GNU g++ 4.4.6 20120305 (Red Hat 4.4.6-4), I would say that my compiler is also supported by that version of Boost.



It's not the library, not the OS, not the compiler: There is a bug in the example program.



It is clear from its reliance on a Windows only function, Beep(), that it has been compiled, linked, and run only on Windows, but as my original post makes clear, the problems go beyond merely that.



Can you run it under Windows as well?




No, I haven't tried because I haven't had a copy of Windows since 1986. It was a less than useful Windows 2 as I recall.



If it is giving problems we will fix it.




It is clearly not being compiled, linked, or running at all.



I would describe that as giving problems.



Please, fix it.
abl
 
Posts: 6
Joined: Thu Feb 28, 2013 3:42 am

Postby Cuchulainn » Tue Mar 05, 2013 4:54 pm

We ran the program on Windows and it works fine. Seems like a buffer problem you are experiencing.



We'll get back on this issue.
User avatar
Cuchulainn
 
Posts: 668
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Postby abl » Tue Mar 05, 2013 5:10 pm

We ran the program on Windows and it works fine. Seems like a buffer problem you are experiencing.




Of course, it ran fine under Windows! I wouldn't expect anything less.



That is what you wrote it for; is it not? :)



Try compiling, linking, and running it under any 64-bit Linux distribution with a recent version of Boost installed by hand instead using whatever old version the distribution's repository may have.



We'll get back on this issue.




Thank you for the attention that you are giving to this matter.
abl
 
Posts: 6
Joined: Thu Feb 28, 2013 3:42 am

Postby Cuchulainn » Wed Mar 06, 2013 9:46 am

You're welcome. And thanks again for the feedback.



For Volume III (libs, design and applications) we will deliver the code for both linux and Windows.
User avatar
Cuchulainn
 
Posts: 668
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Re: CRC Checksums and Time-outs sample code won't compile or

Postby robert2098 » Thu Mar 07, 2013 11:17 pm

Hi abl,



Thanks for reporting the problem.



abl wrote:
Beep() is a Windows function, but I'm running on Linux. I know how to make noise on Linux with echo, but I don't need that to see the program work. I comment out the lines with Beep() in them:





Yes, the Beep function is a Windows function and is used so you can hear that a keep alive is received. You can safely comment it out.





abl wrote:
The compiler can't find toupper. That is not a problem because I can live without a translation to upper case as evidence that a message reached the server.





Actually the problem is that the compiler finds two overloads of "toupper()". One in <locale> with two arguments and one in <cctype> with one argument. When passing the "toupper()" function (function pointer) as argument to the "transform()" function the GNU compiler can't deduct that the version with one argument is meant while the Visual C++ compiler can. The solution is to wrap the call to toupper() in another function that has no conflict.

Thus add the following code to the "Server.hpp" file in the ASyncConnectionMt class:



Code: Select all

   // There are two toupper() functions, one in <locale> and one in <cctype>.

   // When passing a pointer to the toupper() function to the std::transform() function,

   // the GNU g++ compiler can't choose which function to take and gives a compiler error. Microsoft Visual C++ can take the right version.

   // By wrapping it in another function

   static char myToUpper(char c)

   {

      return std::toupper(c);

   }





And replace the line with the "transform()" function with:



Code: Select all

std::transform(str.begin(), str.end(), str.begin(), &ASyncConnectionMt::myToUpper);







abl wrote:
terminate called after throwing an instance of 'std::length_error'

what(): basic_string::_S_create



terminate called after throwing an instance of 'std::bad_alloc'

what(): std::bad_alloc

Aborted







This is caused in how the GNU and Visual C++ compilers differ in handling the length of data types in 64bit code. With the transition to 64bit code Microsoft explicitly kept the length of integer data types the same for compatibility reasons. Thus under both 32 and 64 bit Visual C++ compilers and 32 bit GNU compiler the data type have the following lengths:

short = 16 bit

int = 32 bit

long = 32 bit

long long = 64 bit



But under Linux/GNU, the 64 bit compilers changed the length of data types to:

short = 16 bit

int = 32 bit

long = 64 bit

long long = 64 bit



This caused a problem in building the byte array from the message data for sending a message over the network because it assumed a long was 32 bit.

The solution is to find and replace all the "long"s in the "Message.hpp" file by 'int32_t' which is normally defined in <stdint.h> header file. But this header file is not included in Visual C++ 2008 and lower but luckily it is also defined in the boost libraries <boost/cstdint.hpp> so with boost applications it should also work under older Visual C++ version.



I hope I've answered your questions and that this solves your problems.



Regards,



Robert
robert2098
 
Posts: 2
Joined: Thu Dec 27, 2007 3:12 pm

Postby Cuchulainn » Fri Mar 08, 2013 12:26 am

Thanks for the detailed answer, Robert.



Compiler differences it looks like? OP can now try this out.



Daniel
User avatar
Cuchulainn
 
Posts: 668
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Re: CRC Checksums and Time-outs sample code won't compile or

Postby Camron70 » Fri Oct 17, 2014 7:53 pm

The sample code from book 2, chapter 10, section 6, CRC Checksums and Time-outs fails to compile or even run under CentOS release 6.3 (Final) -- CentOS is essentially RedHat -- with GNU g++ 4.4.6 20120305 (Red Hat 4.4.6-4) and with BOOST_VERSION 105200 and BOOST_LIB_VERSION "1_52" from Boost version.hpp:
Camron70
 
Posts: 1
Joined: Fri Oct 17, 2014 12:31 pm

Re: CRC Checksums and Time-outs sample code won't compile or

Postby Cuchulainn » Mon Oct 20, 2014 8:18 am

What is the precise error message? Which version of Boost?
User avatar
Cuchulainn
 
Posts: 668
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands


Return to Introduction to the Boost C++ Libraries - Volume II (Demming/Duffy)

Who is online

Users browsing this forum: No registered users and 1 guest