Web Programming using C++

How can I create a website using c++?. I've been given a task by a company. I never created an application using c++. I just want a headstart on how to go about this task. Can anyone help me? Her is the task


What sort of task do I need to do in backend with C++?. They never mentioned anything related to the database. So the data will be available via that API.So do I need to call and fetch data from that API through C++? I'm confused please help!.
Last edited on
That is far too generic of a question to get any reasonable answer other than "you can't."

Most websites are written in HTML, maybe with some programmed CGI helpers such as PHP, and graphics/animation thrown in for glitz.
You absolutely could write large parts (if not all) of the backend in C++, although it might be slower (EDIT: to write) than using a language with a much more developed ecosystem for webdev.

As far as the actual data sent to the browser goes, that's indeed a lot more limited. At best, you can compile C++ into WebAssembly using Emscripten.

-Albatross
Last edited on
Writing a C++ webserver is a big task. I wrote one for my hobby computer algebra system and it's pretty large. If you'd like to see the main loop of my server, take a look at:

https://github.com/tmilev/calculator/blob/master/src/webserver.cpp#L4255

for the server and

https://github.com/tmilev/calculator/blob/master/src/webserver.cpp#L4496

for the worker processes. The guide I used to write the first version of this server is here:

https://beej.us/guide/bgnet/html/single/bgnet.html

The most important question you need to answer when writing a C++ server is: what architecture will you use? I know of 3 approaches. Here are they in order of increasing complexity.

- Approach 1. Process incoming connections in separate processes. Use a simple server listen loop (possibly single-threaded, as is the case with my server). Fork out for each new incoming connection. This approach is quite safe, stable, simple, and has good performance too.

I am not 100% sure if that still is the case, but I think this is how the Apache web server works.
This is also how my web server works.
- Approach 2. Same as approach 1, but use multi-threading rather than separate processes to handle new incoming connections.

This has the disadvantage that, if your system is overloaded and crashes, your main server loop burns. If you choose this approach, you **WILL** need to invest in server monitoring/auto-server restart.

- Approach 3. Handle incoming packets as in Approach 2. However, each of you handling threads handles exclusively input/output operations and uses selects/polls to manage multiple open connections at the same time. Once input/output is received:
- pile all TCP packets onto a queue of unprocessed messages
- offload message processing to worker threads.
- When worker threads are done, they pile back their ready-to-be-sent packets in a processed messages queue.
- Those are in turn consumed by your input/output threads.

From what I understand - please do correct me if I am wrong - the Node.js server (whose core is written, I heard, in C++) has an architecture along these lines.
Last edited on
First, if you're new to C++, then this isn't a project you can reasonably expect to accomplish for a project given to you by a company. There's too much to learn.

If you know another language, and could opt to use another language, let us know what you do know.

Learning C++ from zero takes at least several months to a year, and that's with dedicated time. For mastery, one estimate given decades ago was 18 months, but C++ has grown considerably in that time. In some ways it is simpler now, but to some extent there's more available to learn.

That said, what I didn't see in these other posts is the notion of CGI (Common Gateway Interface). This interface is available on most existing web servers which allows applications written in native languages (like C++) to be used as the backend to a web server. This is simpler than what @tition mentions, where that approach is basically writing the web server portion. That's a larger task, fraught with security issues you wouldn't want to create for your customer.

Instead, CGI does for C++ what PHP does, in that the web page being presented is "connected" to small C++ command line applications which perform the backend creation. In PHP, the task is to respond to a request by generating HTML text using logic (and often database access). CGI allows C++ to do something similar, which is a much smaller (and far more secure) task than writing an entire web server.

It also makes the programs simpler to write, such that a student can rise to the occasion, but I still don't think it is simple enough for someone with zero C++ experience. You would at the very least require about 6 months of study and practice to be up to that task.

In theory, CGI can use any language capable of generating a console or command line executable.

On a closing note, database access is not provided by any web interface. That is a language or library specific feature. PHP, for example, has built in support for connecting to various SQL database engines. It is likely SQL of some form is what you require for database access. For C++ you require a library to connect to and query the database. This is a separate study from CGI, and something quite later in the study for a beginner - it is, however, merely in the intermediate level of C++.
Last edited on
@Niccolo tition’s response was absolutely correct; he was talking about CGI.

Also, you do not need to learn everything there is to know about C++ in order to properly create a CGI executable. Any valid C++ program will do.

@Srinath48 Niccolo is absolutely correct that you have a very steep learning curve ahead of you. Online resources to learn safe CGI are scarce.

You must have:

  • A database that meets your functional requirements AND has a C (and maybe C++) interface
  • A dedicated HTTPS server (no sharing! shared webservers are notoriously insecure!)
  • A good tutorial on CGI (google around “C CGI” and the like)
  • A good grasp of how to validate and process session tokens
  • A development environment that exactly duplicates your production environment

The idea is that when someone wants to view your site, your server first runs your chosen executable, and said executable generates the HTML to send to the user. Further submissions send back to the executable (or even a different executable, as you wish).

Prefixed to the HTML stream is a list of “header” data, including session cookies and the like. It must be exactly formatted and your executables must know how to parse the environment to get the data and verify session tokens, and prefix the proper data to the HTML each time it produces output. (That’s right, your CGI EXE is a text-mode application.)

You probably want to use a library to handle the security/header tokens for you, too.
Topic archived. No new replies allowed.