• Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
  • Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

Making a C++ web server

Collapse
X
  •  
  • Filter
  • Time
  • Show
Clear All
new posts

    Making a C++ web server

    Anyone here ever done it?
    "He's actually ripped" - Jared Padalecki

    https://youtu.be/l-PUnsCL590?list=PL...dNeCyi9a&t=615

    #2
    Yes.
    Will work inside IR35. Or for food.

    Comment


      #3
      Don't forget to do your

      ./configure or your make will fail.
      And the lord said unto John; "come forth and receive eternal life." But John came fifth and won a toaster.

      Comment


        #4
        I did a very simple one in perl once, based on a similar SMTP server that someone else had written. Never contemplated doing it in C or C++ though I've written other network server code.

        TBH if it's for anything other than the learning experience I'd consider using apache instead, if it's for a custom project you can easily (well more easily than writing your own from scratch) build a cut down / spiffy version with whatever modules you need and hook your own modules in if you need custom functionality.

        Either way, I would recommend looking at the apache source code and the Richard W Stevens TCP/IP books,
        While you're waiting, read the free novel we sent you. It's a Spanish story about a guy named 'Manual.'

        Comment


          #5
          nginx is also worth a look. Source, About.

          Comment


            #6
            I know its not C++ but if you ever need to do anything stupid with a web server just use Node..
            merely at clientco for the entertainment

            Comment


              #7
              Good to see my reply was the most helpful.

              I found a sample one somewhere that I modified to my own ends; but it was only a simple single threaded server that supported GET and nothing else. But I plan on replacing it with libwebsockets, which as well as doing web sockets supports http/https requests. Obviously it depends how sophisticated you need it to be.
              Will work inside IR35. Or for food.

              Comment


                #8
                I write them in REXX, even managed to get a multi-threading one working!
                Brexit is having a wee in the middle of the room at a house party because nobody is talking to you, and then complaining about the smell.

                Comment


                  #9
                  Originally posted by darmstadt View Post
                  I write them in REXX, even managed to get a multi-threading one working!
                  So I decided to use multithreading. Now I problems havtwo e.
                  Behold the warranty -- the bold print giveth and the fine print taketh away.

                  Comment


                    #10
                    /*
                    /*
                    Simple HTTP Web Server
                    By : Kartik Kumar Perisetla([email protected])
                    hosted for kartik::shareWith("The World!");
                    */

                    //for windows
                    #include<winsock2.h>

                    // for POSIX based systems use- #include<sock.h> in place of #include<winsock2.h>

                    #include<sys/types.h>
                    #include<stdio.h>
                    #include<string.h>
                    #include<stdlib.h>
                    #include<unistd.h>
                    #include<errno.h>
                    #pragma comment(lib, "Ws2_32.lib")


                    //function specific for Windows-to be called before invoking call to socket()
                    int init()
                    {
                    WSADATA wsaData;
                    int iResult;

                    // Initialize Winsock
                    iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
                    if (iResult != 0)
                    {
                    printf("WSAStartup failed: %d\n", iResult);
                    return 1;
                    }

                    }

                    //function specific for windows-to be called after all socket communication is complete
                    void clean()
                    {
                    WSACleanup();
                    }
                    int main()
                    {


                    while(1)
                    {
                    init();
                    server();
                    clean();
                    }

                    return 0;
                    }

                    void server()
                    {

                    int sock, connected, bytes_recieved , true = 1;
                    char send_data [1024] , recv_data[1024];

                    struct sockaddr_in server_addr,client_addr;
                    int sin_size;

                    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
                    perror("Socket");
                    exit(1);
                    }

                    if (setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&true,siz eof(int)) == -1) {
                    perror("Setsockopt");
                    exit(1);
                    }

                    server_addr.sin_family = AF_INET;
                    server_addr.sin_port = htons(8080);
                    server_addr.sin_addr.s_addr = INADDR_ANY;
                    //bzero(&(server_addr.sin_zero),8); --This is for POSIX based systems
                    memset(&(server_addr.sin_zero),0,8);
                    if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr))== -1)
                    {
                    perror("Unable to bind");
                    exit(1);
                    }

                    if (listen(sock, 5) == -1)
                    {
                    perror("Listen");
                    exit(1);
                    }

                    printf("\n\nMyHTTPServer waiting on port 8080");
                    fflush(stdout);


                    sin_size = sizeof(struct sockaddr_in);

                    connected = accept(sock, (struct sockaddr *)&client_addr,&sin_size);

                    // printf("\n I got a connection from (%s , %d)",
                    // inet_ntoa(client_addr.sin_addr),ntohs(client_addr. sin_port));

                    char kk[9999];
                    recv(connected,kk,sizeof(kk),0);
                    printf("\n Received:%s",kk);

                    char xx[9999];
                    strcpy(xx,"HTTP/1.1 200 OK\nContent-length: 47\nContent-Type: text/html\n\n<html><body><H1>Hello Kartik</H1></body></html>");

                    int c=send(connected,&xx,sizeof(xx),0);
                    printf("\nSTATUS:%d",c);

                    printf("\nSent : %s\n",xx);

                    close(sock);
                    WSACleanup();
                    }

                    // Doesn't do much - only echoes back Hello Kartik !
                    // See Write a Simple HTTP-based Server Using MFC and Windows Sockets for a more complete (although very old) C++ web server !!

                    Comment

                    Working...
                    X