NetCpp  v0.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Macros Pages
NetCpp


A tiny C++ network library

Author
Evidence Srl - www.evidence.eu.com



Table of contents

  1. Rationale
  2. Requirements
  3. How to build the library
  4. Usage
  5. Supported platforms
  6. Adding further network protocols
  7. License
  8. Support
  9. To do



Rationale

The C++ standard library is known to be less comprehensive than the libraries traditionally available for other programming languages (e.g., Java, Python, etc.). The C++11 standard began to fill this gap by adding several features like support for multithreading, smart pointers, etc. Incredibly, however, a basic support for networking is still missing.

During the last years, a number of C++ libraries have been proposed to have portable networking in C++, the most important being Boost::asio and cpp-netlib. However, we believe that the syntax of both these libraries is still more complicated than necessary. In particular, we believe that an ideal C++ networking library should be:

  • easy to use
  • easy to extend (either in terms of protocols and of supported architectures)

NetCpp is a proof of concept to show that such a networking library can be made easily designed in C++. This tiny library has been built after the experience with the OnPosix library. The syntax of the library is straightforward. The modular design given by the adoption of design patterns (i.e., Abstract Factory and Bridge) allows to easily add new protocols and supported platforms.



Requirements

To build NetCpp you need both:

  • cmake
  • a C++11 compiler (e.g., gcc 4.7+ or LLVM 3.3+)

Currently, only Posix systems (e.g., Linux). However, porting the library to different platforms is very easy thanks to its modular design.



How to build the library

Compile through the following commands:

git clone https://github.com/claudioscordino/netcpp.git
cd netcpp/build
cmake ..
make
make install

The library is put in the netcpp/bin/ directory. The include files are available in the netcpp/include/ directory.

Documentation is generated through Doxygen. To generate the documentation, type:

git clone https://github.com/claudioscordino/netcpp.git
cd netcpp/build
cmake ..
make doc

Start reading documentation by opening doc/html/index.html.



Usage

Usage of the library is straightforward:

TCP client/server

Example of TCP server:

net::ip4::tcp::address addr (std::string("127.0.0.1"), 1234);
net::ip4::tcp::server srv(&main_srv); // accept()
srv.open(&addr);
std::array<char, 5> buf;
srv.receive(net::buffer(buf), 5);

Example of TCP client:

net::ip4::tcp::address addr (std::string("127.0.0.1"), 1234);
clt.open(&addr);
std::array<char, 5> b {'h', 'e', 'l', 'l', 'o'};
clt.send(net::buffer(b), 3);

UDP client/server

Example of UDP server:

net::ip4::udp::address addr (std::string("127.0.0.1"), 1234);
srv.open(&addr);
std::array<char, 5> buf;
srv.receive(net::buffer(buf), 5);

Example of UDP client:

net::ip4::udp::address addr (std::string("127.0.0.1"), 1234);
clt.open(&addr);
std::array<char, 5> b {'h', 'e', 'l', 'l', 'o'};
clt.send(net::buffer(b), 3);



Supported platforms

The library has a modular internal design which, thanks to the design patterns used, allows to decouple specific platform-dependent code (which is on the net::AbstractSystemSocket hierarchy) from any protocol design (which is on the net::AbstractSocket hierarchy). Modularity is especially given by the Bridge pattern, which allows two class hierarchies to vary independently.

Thus, to add a new supported platform, you only need to:

  1. inherit your platform-specific class from class net::AbstractSystemSocket.
  2. change the concrete class allocated by net::createSocket().

The build system, based on cmake, can be easily ported among all supported platforms.

See the following picture to understand the collaboration between classes:



Adding further network protocols

To add a new protocol:

  • Create a proper namespace (like net::ip4::tcp)
  • Then, inside the new namespace:
    • Create an address class inheriting from class Address
    • Create server and client classes inheriting from class net::AbstractSocket



License

The library is under the Boost license. Read the LICENSE file for more information.



Support

For reporting bugs or proposing new patches, use the issues link of GitHub.



Todo

  • Add recvfrom and sendto for dgram connections
  • Add asynchronous operations