3

I understand that as C++ isn't dynamic, its not seen as a particularly great base to build a website, but I believe it partly falls down to support. Are there any servers that run C++ programs as like a root, for example node.js creates the server and then handles all requests that go to the root yet CGI (which I don't want to use) is like a Php webpage, limited to slow Apache with annoying re-writes. Are there any C++ librarys that let you easily embed a server.

Will03uk
  • 3,346
  • 8
  • 34
  • 40
  • 2
    This question is all over the place; could you perhaps focus on one or two small aspects of it, and try to be more specific? (Even more specific questions might be closed as "too subjective", but something like "I'm having trouble getting libev's web server to work with my C++ project,
    , are there better choices?" would probably get real answers.)
    – sarnold Apr 19 '11 at 01:34
  • Not sure if Wt will fully address your requirements. If you were not aware of it, do check it out: http://www.webtoolkit.eu/wt – yasouser Apr 19 '11 at 02:17
  • @Wt to abstracted and I would prefer to use something like haml to template my pages, which is actually available, google hardcore-haml – Will03uk Apr 19 '11 at 09:14

2 Answers2

5

I recently (as in two days ago) had to rewrite a Java Jetty servlet in C++ (due to some JNI issues with OpenCV which I was also using). I had the exact same question as you and there's no easy answer (I also didn't want to use CGI). I suggest a couple of things:

Use boost, use boost, use boost.
Boost makes cross-platform deployment absolutely trivial and (dare I say it) fun! My dev machine is running Windows 7 but I had to deploy the server on a Linux server and boost made it incredibly easy.

Use an HTTP library
For my project, I found cpp-netlib, which makes writing servlets (if you can even call them that) very very easy. It offers access to request and response objects and even supports multithreading. Here's the server example from their website:

#include <boost/network/protocol/http/server.hpp>
#include <string>
#include <iostream>

namespace http = boost::network::http;

struct hello_world;
typedef http::server<hello_world> server;

struct hello_world {
    void operator() (server::request const &request,
                     server::response &response) {
        std::string ip = source(request);
        response = server::response::stock_reply(
            server::response::ok, std::string("Hello, ") + ip + "!");
    }
};

int
main(int argc, char * argv[]) {

    if (argc != 3) {
        std::cerr << "Usage: " << argv[0] << " address port" << std::endl;
        return 1;
    }

    try {
        hello_world handler;
        server server_(argv[1], argv[2], handler);
        server_.run();
    }
    catch (std::exception &e) {
        std::cerr << e.what() << std::endl;
        return 1;
    }

    return 0;
}

Very neat-looking and (from what I've seen) it's also very robust. Do not (do NOT) write your own HTTP 1.x implementation. I strongly advise against this because it's a huge time investment and there's no reason to reinvent the wheel.

Do what works for you
If you need your server to have some hot-swappable parts, feel free to use an easy scripting language like LUA - otherwise, just be ready to compile at every iteration.

David Titarenco
  • 32,662
  • 13
  • 66
  • 111
1

I understand that as C++ isn't dynamic

It's not, which means any code you write for a site will have to be compiled in a manner compatible with the server's packages and OS, and recompiled every time you change it. You probably wouldn't want to recompile on the production server, so you have the possible overhead of another identical server (you may already have that for testing, which would make it a non-issue).

You can write CGI scripts in C or C++, when you need performance from that bit. There are a few libraries that can simplify that and you should be able to find tutorials (a quick Google search brought up this).

You can also write modules to extend your script language in some cases. I know PHP is mostly implemented in C underneath, and I believe most other script languages allow for C modules to be added (C++ may be somewhat more difficult, but should be doable with care or a wrapper). You may look into that.

I would not recommend using Qt for a site, it seems not really intended for that and potentially carrying a lot of things you don't need. If you were going this route, simpler C/C++ would be better (and faster).

In comparison to scripting languages (which is somewhat opinion), you can probably expect carefully written and optimized C/C++ to be somewhat faster and have less memory use, but it will likely be a wee bit harder to write at first, modify later and possibly debug. It is a viable choice, though, if your problem calls for it.

ssube
  • 47,010
  • 7
  • 103
  • 140
  • +1, but with a comment: I'd go for FastCGI rather than CGI. The performance hit from spawning a separate child process under CGI is huge. – Sherm Pendley Apr 19 '11 at 01:50
  • The performance hit for using CGI is huge with languages and operating systems that make it huge. Java for CGI programs is a bad thing. Windows and Solaris are slow to start a process, Linux and the BSDs are not. If you are using C++, I expect the CGI overhead to be tiny unless your code is doing too much, to the point where the overhead of not using CGI might not be worth it until you're doing millions of hits a day. Static linking helps, too. As always, profile, don't speculate. Also, see http://cr.yp.to/publicfile/performance.html – janm Apr 19 '11 at 01:57