• 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

5.00 star(s)
Don't forget to do your

./configure or your make will fail.
 
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,
 
I know its not C++ but if you ever need to do anything stupid with a web server just use Node..
 
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.
 
I write them in REXX, even managed to get a multi-threading one working!
 
/*
/*
Simple HTTP Web Server
By : Kartik Kumar Perisetla(kartik.peri@gmail.com)
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,sizeof(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 !!
 

Advertisers

Back
Top