Help streaming over http in C++

浪子不回头ぞ 提交于 2019-12-12 08:57:20

问题


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.


回答1:


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




回答2:


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()



来源:https://stackoverflow.com/questions/2242687/help-streaming-over-http-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!