Baruch
Baruch

Reputation: 21548

Boost - ASIO vs. IOStreams TCP

I am new to Boost. I was looking for an easy cross-platform solution for sokcets/networking/TCP stuff and found Boost. A quick peek shows there seems to be two TCP related classes: One in Iostreams and one in Asio.
I am (pretty) sure if I dig into the respective documentations of both libraries I will be able to figure out the use of each one, but can someone explain in short what the difference is, or what each is used for?

Upvotes: 2

Views: 4779

Answers (3)

authchir
authchir

Reputation: 1635

As other have said, Boost.Asio is probably what you want. It is an elegant and cross-platform wrapper for system specific networking stuff. It offer building block such as socket, IP address, timers, etc.

But it also offer a high level iostream interface for simple networking interactions. Here is a simple example:

#include <iostream>
#include <string>
#include <boost/asio.hpp>

using boost::asio::ip::tcp;

int main(int argc, char* argv[])
{
  try
  {
    if (argc != 2)
    {
      std::cerr << "Usage: daytime_client <host>" << std::endl;
      return 1;
    }

    tcp::iostream s(argv[1], "daytime");
    if (!s)
    {
      std::cout << "Unable to connect: " << s.error().message() << std::endl;
      return 1;
    }

    std::string line;
    std::getline(s, line);
    std::cout << line << std::endl;
  }
  catch (std::exception& e)
  {
    std::cout << "Exception: " << e.what() << std::endl;
  }

  return 0;
}

Upvotes: 3

Ferruccio
Ferruccio

Reputation: 100758

I've used Boost IOStreams to easily create std::stream compatible stream objects. You can use them to create a TCP stream class, but you will be doing all the work to support TCP. IOStreams just provides a framework to create stream classes.

I've also used Boost Asio to create a stand-alone TCP server. Having used Windows sockets in the past to do the same sort of thing, I can tell you Asio makes writing TCP servers (and clients) really easy. I think Asio is what you want.

Upvotes: 4

Jonathan Wakely
Jonathan Wakely

Reputation: 171443

Is there a TCP stream in Boost.Iostreams?

ASIO is a complete full-featured networking library supporting asynchronous I/O using a generic callback API. The ip::tcp::iostream class (which is part of ASIO) is built on top of ASIO, hiding much of the complexity of creating and managing a socket manually and providing a standard iostream interface.

Upvotes: 5

Related Questions