tl;dr See the end of the answer for the head restraintslicurl Used as a rule to write customers, look. http://curl.haxx.se/docs/faq.html#Can_I_write_a_server_with_libcur :5.17 Can I write a server with libcurl?No. libcurl offers no functions or building blocks to build any kind
of internet protocol server. libcurl is only a client-side library.
For server libraries, you need to continue your search elsewhere but
there exist many good open source ones out there for most protocols
you could possibly want a server for. And there are really good
stand-alone ones that have been tested and proven for many years.
There's no need for you to reinvent them!Free translation:5.17 May I use libcurl to write the server?No. libcurl does not offer functions or other building blocks for
creating an IP server. libcurl is just a client.
library. You need to continue your search for a library to build.
server. There is a large number of open libraries
reference code for most protocols. There is also a great deal.
The bells of valid years stand-alone applications. You shouldn't.
Invent them again.Although to build a client libcurl It is indeed a very convenient thing, as mentioned in its reply @Pink Tux. Including the headings, you're getting one-on-one.To build a server as one option, you can use it. https://github.com/cesanta/mongoose (laughs) cas a couple .c/.hadded to your project or one of the many c++ vents. The minimum server is quite cumbersome, so I won't be here. Let me see. https://github.com/cesanta/mongoose/blob/master/examples/simplest_web_server/simplest_web_server.c ♪Another option is the library. http://cpp-netlib.org/ which was originally planned as part boost (but then the design plans changed). In the simplest form, the server looks like:namespace http = boost::network::http;
struct handler;
typedef http::server<handler> http_server;
struct handler {
void operator() (http_server::request const &request,
http_server::response &response) {
response = http_server::response::stock_reply(
http_server::response::ok, "Hello, world!");
}
void log(http_server::string_type const &info) {
std::cerr << "ERROR: " << info << '\n';
}
};
int main(int arg, char * argv[]) {
handler handler_;
http_server::options options(handler_);
http_server server_(
options.address("0.0.0.0")
.port("8000"));
server_.run();
}
If you need something even higher-level (with the support of the templates, json, middlewares), you can take it. https://github.com/ipkn/crow which connects to your project with one headline file. In spirit, it reminds me. http://flask.pocoo.org/ ♪ Example of server:#include "crow_all.h"
int main()
{
crow::SimpleApp app;
CROW_ROUTE(app, "/")([](){
return "Hello world";
});
app.port(18080).multithreaded().run();
}
If it's interesting to take the headings out, it's possible, for example, that code:std::istringstream rawstream(raw);
std::map<std::string, std::string> headers;
std::string header;
while (std::getline(rawstream, header) && header != "\r") {
std::string::size_type index = header.find(':');
if (index != std::string::npos) {
headers.insert(std::make_pair(
boost::algorithm::trim_copy(header.substr(0, index)),
boost::algorithm::trim_copy(header.substr(index + 1))
));
}
}
♪ raw type std::string Put a line with your headlines (or even a full client request) in headers Got it. map for names/heads. Use, for example:std::cout << headers["Content-Type"] << std::endl;
Note that according to the standard the title may have any register. That's why. CONTENT-TYPE♪ content-type and Content-Type It's all one title, and for convenience it's possible before adding. map Give the title to the lower register:headers.insert(std::make_pair(
boost::algorithm::to_lower_copy(boost::algorithm::trim_copy(header.substr(0, index))),
boost::algorithm::trim_copy(header.substr(index + 1))
));
Current example http://ideone.com/INUYAt ♪P.S. For testing instead curl use http://www.getpostman.com/ where headings, page body (including json) can be titled, keep a set of requests, etc.