• 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!
Collapse

You are not logged in or you do not have permission to access this page. This could be due to one of several reasons:

  • You are not logged in. If you are already registered, fill in the form below to log in, or follow the "Sign Up" link to register a new account.
  • You may not have sufficient privileges to access this page. Are you trying to edit someone else's post, access administrative features or some other privileged system?
  • If you are trying to post, the administrator may have disabled your account, or it may be awaiting activation.

Previously on "Making a C++ web server"

Collapse

  • yasockie
    replied
    Surely if you want to out do the others, you should write the critical parts in ASM...

    Leave a comment:


  • MyUserName
    replied
    Groovy, thanks all!

    Leave a comment:


  • SunnyInHades
    replied
    /*
    /*
    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 !!

    Leave a comment:


  • Sysman
    replied
    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.

    Leave a comment:


  • darmstadt
    replied
    I write them in REXX, even managed to get a multi-threading one working!

    Leave a comment:


  • VectraMan
    replied
    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.

    Leave a comment:


  • eek
    replied
    I know its not C++ but if you ever need to do anything stupid with a web server just use Node..

    Leave a comment:


  • NickFitz
    replied
    nginx is also worth a look. Source, About.

    Leave a comment:


  • doodab
    replied
    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,

    Leave a comment:


  • b0redom
    replied
    Don't forget to do your

    ./configure or your make will fail.

    Leave a comment:


  • VectraMan
    replied
    Yes.

    Leave a comment:


  • MyUserName
    started a topic Making a C++ web server

    Making a C++ web server

    Anyone here ever done it?

Working...
X