12

Google is well known for the ridiculous amount of C++ they've coded over the years. Correct me if I'm wrong, but a large part of Google's core search engine is written in C++, isn't it? How does one take a program written in C++ and interface it with a website?

Note: I'm not looking for how Google in particular does this, just how it might be done in general.

1
  • Well, Google has built most of their application in python and now Java is also seen in use a lot. Google would not be restricting on languages usage. For web interfacing, socket are used. Nothing of C++, but of C apis. C++ does not has its own !! Feb 28, 2013 at 18:18

4 Answers 4

17

Any web software will only send and receive messages through sockets, that's all. You could use any language to do this, it's not specific to languages.

However, you'd better not reinvent the wheel for this kind of work so most languages that are used to do web applications have their set of framework that does the basic communication for you, to allow you to concentrate on the specificities of your project. Ruby have ROR, Python have Django and others, Java as ...etc.

C++ historically didn't have any similar framework until recently:

  • a modern-C++ way of doing it is to use something like CPPCMS;
  • there is also an effort to setup a standard library for web dev. in C++, one of them being cpp-netlib;
  • Recently there have been a release of a cross-platform REST API library for C++11 from Microsoft called Casablanca which also helps;

Now, the "ridiculous amount of C++" that Google is built over is necessary because you need to have very-high-performance modules to solve the kind of problems Google solves. Good luck trying to do the same without any module written in a language focused on performance. I recommend reading the CPPCMS wiki about this subject to understand better. For historic facts, Amazon, Google, Facebook (see Hip Hop and recent Alexandrescu interviews) and some other really big web services do have cores in C++, for obvious computational reasons that are more important than the time lost on programmer productivity.

CPPCMS and cpp-netlib being open source, you can study them if you want to know how to make an application work as a web service using C++. That said, any application that can listen to ports and send data to port can potentially do this, it's all about protocoles (TCP/IP, HTTP, etc.), not code.

4
  • Can't upvote quiet yet, but I've accepted your answer. I wasn't aware anything like CPPCMS existed, but it's good to know. Thanks! May 6, 2012 at 8:51
  • 1
    I understand, it's pretty new compared to the others. Other poor (in my own advice) solutions existed previously but I think this one is the more suited to the language and the problem.
    – Klaim
    May 6, 2012 at 8:57
  • 1
    C++ did have such frameworks - the web used to be run entirely with cgi apps, or MS's ISAPI dlls, or even Microsoft's ATLServer. Nowadays, we have several c++ web frameworks, and Microsoft is working on Casablanca, which gives you c++ as cloud services.
    – gbjbaanb
    May 6, 2012 at 13:41
  • It should be mentioned that most web applications live behind a front end server and talk with it either CGI, which passes the data in environment, stdin and stdout or FCGI, which uses sockets and somewhat simpler protocol than HTTP.
    – Jan Hudec
    Mar 1, 2013 at 8:29
3

Like any other language....

Open a TCP socket, send and receive http messages. Parse them and do stuff based on content.

Look at Qt for an example on a c++ framework that supports some basic http functionality.

There are also dedicated web frameworks for C++. Not many nor widely used, AFAIK. Probably because if you do web via C++, you are doing something very specialized anyway and want full control over everything.

4
  • I probably should have been able to figure this out for myself. The only web programming I've done has been through Django, so it was all abstracted away. May 6, 2012 at 6:53
  • I figured that. Django is nice, but if you dig down into it you'll see that the core part is not very tricky. Now, writing a Django equivalent in C++, there's a fun challenge. ;)
    – Macke
    May 6, 2012 at 6:58
  • @Macke Well I guess CPPCMS is a fun challenge, not sure though. The lack of standard networking in C++ don't help but there are tons of solutions to work with it. The main problem with C++ I guess is more the fact that it's hard to make updates without restarting the executable, and it's also so long to compile sometimes. CPPCMS helps with that though.
    – Klaim
    May 6, 2012 at 8:43
  • Btw, for a modern, good async networking c++ library, I recommend the ASIO library, available as a part of the excellent Boost package. I've writtens some simple TCP/UDP client/server apps in it, and it's quite beautiful.
    – Macke
    May 6, 2012 at 9:40
2

From a client perspective, if you have a C/C++ program and you want to use it to talk to the internet you'd use libcurl. It's pretty much a standard (and comprehensive) library to connect to http urls to send and receive data. There are several other libraries - Microsoft has a few, there's a several open source ones.

Now that's for plain old URLs over http (or SMTP, or FTP, or Gopher, or.. libcurl is very comprehensive!) If you're trying to consume web services over SOAP, then you can use one of many other libraries such as gsoap.

For the server side, you need a webserver or framework that exposes your C++ code. Again there are many way to do this - the earliest web programs were all C/C++, written as CGI programs where the webserver called your app using a form of IPC. Since then, we have Wt, Qt, cppCMS, ffead, and all the ones from Microsoft (ISAPI, ATLServer, and now Casablanca).

I think the reason no-onne really uses C++ for their web progrmaming is because the other languages come pre-packaged with these libraries. C++ (as always) you have to go find a library to use, whereas PHP just comes ready to go for the web.

-4

Client-server architecture lets this happen.

C++ may be used to develop the server.

On the client side, web technologies may be used to communicate with the server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.