views:

385

answers:

2

I'm looking to use a web service that offers a streaming api. This api can typically be used by the java method java.net.URL.openStream();

Problem is I am trying to design my program in C++ and have no idea what libraries (I've heard the cUrl library is very good at this sort of thing) to use, or how to use them to do what I want.

The idea is that after opening the file as a stream I can access continually updating data in realtime.

Any help would be much appreciated.

+2  A: 

Boost.Asio socket iostreams seem to be what you're after. Your code will look like this:

ip::tcp::iostream stream("www.someserver.com", "http");
if (!stream)
{
  // Can't connect.
}

// Use stream as a regular C++ input stream:
std::string text;
std::getline(stream, text);

If you're new to C++ and have no experience with iostreams then this page is an excellent source of information. In particular, check the docs of the istream class to see what kind of operations your Boost.ASIO stream will support. You'll find that they're not so different from those in the Java IO API.

EDIT:

Eric is right, you'll have to send some requests to the server (using the same stream) so it's probably less similar to Java's openStream than I thought. The following example shows how to make those requests:

http://blog.think-async.com/2007_01_01_archive.html

Manuel
For your example to work, I think you'd have to write a "GET" request on the stream before reading from it. Something like: `stream << "GET /index.html HTTP/1.0\r\n\r\n";`
Éric Malenfant
Thanks Éric, I had missed that.
Manuel
U guys rock ... ill keep you updated on the progress
Travis
+3  A: 

It depends what you're after. Manuel's suggestion of boost::asio::ip::tcp::iostream is good if you want something at a lower level, directly returning the "raw" server response (However, I suspect that something is missing in the example provided in his answer: I think that a "GET" request should be written to the stream before reading from it. See this example from the Asio docs).

I have no experience with java.net.URL.openStream(), but it seems that it is at a little higher level in that only returns the body (and not the headers) of the reply, takes care of HTTP redirects, etc. In that case, yes, libcurl may be more what you want. You could also take a look at the cpp-netlib library, which is built on top of Boost.Asio. It is still in its infancy, but its http::client seems to already provide something pretty similar to what is provided by Java URL.openStream()

Éric Malenfant