Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm currently working on a Computer Science degree, and I want to build my own Web Server library. The problem? Every search I've done on interacting with web sites points to "Use this library that was built by someone else" - instead of explaining the basics of sending and receiving information.

I'm not asking for a lot - I just want to enter a single alpha-numeric character on a web page, hit a button, and have that one byte of information sent to a C++ program, without the aid of a non-standard library.

share|improve this question

closed as unclear what you're asking by gnat, MichaelT, Kilian Foth, Jimmy Hoffa, Yusubov Jul 18 '13 at 12:33

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

3  
Have you considered apache (or most any other web server) and the common gateway interface? Or apache with a c++ plugin (associated tutorials)? –  MichaelT Jul 16 '13 at 0:09
    
Another suggestion would be Mongrel 2. It just sends requests to the target program and recieves the output through ZeroMQ sockets. –  Will Jul 16 '13 at 0:19
    
There's also boost ASIO if you really want to start low level –  jozefg Jul 16 '13 at 0:57
    
+1 for Great Question. –  Md. Mahbubur R. Aaman Jul 16 '13 at 13:42
    
This should REALLY be moved over to stackoverflow.com –  rubber boots Jul 16 '13 at 16:13

3 Answers 3

Boost.Asio is currently being converted to be (one of?) the future C++ standard way to do networking. (Here is one of the papers: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3388.pdf )

It's really the most basic portable asynchronous communication system you will find using modern C++, as it abstract away other socket libraries or platform provided ones.

All the modern C++ http networking libraries (CppNetlib and CPPCMS) relies on it. I suggest you build your library around it provide basics for sockets and asynchronous (and synchronous) comunication but don't setup a full system for you. Maybe in a few years you can even replace Boost.ASIO by the C++ standard equivalent.

Also, the documentation of Boost.Asio provide insight on how communication works in this kind of context. So reading it might answer a lot of your questions.

And if you want to understand the lower level, read the Asio code. It's a bit hard to read if you're not used to C++ meta-programming and other C++ idioms but you would learn a lot by reading it.

share|improve this answer

Some of tye info that you want is here - http://en.wikipedia.org/wiki/Common_Gateway_Interface . It might not sound like what you are trying to do is very complicated, but it's a bit more complicated than you might think. As Dominic said the tricky part will be opening the port and reading from the socket - quite difficult in c++ land.

I would recommend that to start with, use an existing web server and write your own CGI module. The web server will take care of the sockets side of things, and basically you're just reading data from environment variables or command line arguments (if it's a GET request) or from the stdin if it's a POST (I think, this is data from my memory that's about 15 years old). What you want is something like this:

#include <stdio.h>
#include <string.h>

int main(){
    char *s=getenv("CONTENT_LENGTH");
    int i=atoi(getenv("CONTENT_LENGTH"));
    printf("Content-type: text/html\n\n");
    printf("%s\n<br />",s); //Shows you CONTENT_LENGTH works
    printf("%d\n<br />",i); //Shows you it was converted to int
    char *tmp = new char[100];
    fread(tmp,i,1,stdin); //read from stdin something of i bytes to tmp
    printf("%s\n<br />",tmp);
    return 0;
}

BTW as a lesson for the reader this application has a buffer overflow bug.. you probably want to fix that :)

share|improve this answer

Correct me if I'm wrong, but it sounds like you want to program your own web server from scratch, using only standard C++ components.

First of all you are in a bit of a bind as there is no standard tcp/ip socket library, so you'll need to use Windows sockets, or Posix sockets depending on platform.

Then you'll need to research the format of requests/responses. I'm sure with a bit of searching you'll find a resource on the http protocol.

After that you'll need to create a server that listens for connections, responds to requests and sends responses.

If that isn't what you are asking, then MichaelT's suggestion sounds reasonable.

share|improve this answer

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