- 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!
Reply to: Making a C++ web server
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.
Logging in...
Previously on "Making a C++ web server"
Collapse
-
Surely if you want to out do the others, you should write the critical parts in ASM...
-
/*
/*
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:
-
I write them in REXX, even managed to get a multi-threading one working!
Leave a comment:
-
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:
-
I know its not C++ but if you ever need to do anything stupid with a web server just use Node..
Leave a comment:
-
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:
-
Making a C++ web server
Anyone here ever done it?Tags: None
- Home
- News & Features
- First Timers
- IR35 / S660 / BN66
- Employee Benefit Trusts
- Agency Workers Regulations
- MSC Legislation
- Limited Companies
- Dividends
- Umbrella Company
- VAT / Flat Rate VAT
- Job News & Guides
- Money News & Guides
- Guide to Contracts
- Successful Contracting
- Contracting Overseas
- Contractor Calculators
- MVL
- Contractor Expenses
Advertisers
Contractor Services
CUK News
- Streamline Your Retirement with iSIPP: A Solution for Contractor Pensions Sep 1 09:13
- Making the most of pension lump sums: overview for contractors Sep 1 08:36
- Umbrella company tribunal cases are opening up; are your wages subject to unlawful deductions, too? Aug 31 08:38
- Contractors, relabelling 'labour' as 'services' to appear 'fully contracted out' won't dupe IR35 inspectors Aug 31 08:30
- How often does HMRC check tax returns? Aug 30 08:27
- Work-life balance as an IT contractor: 5 top tips from a tech recruiter Aug 30 08:20
- Autumn Statement 2023 tipped to prioritise mental health, in a boost for UK workplaces Aug 29 08:33
- Final reminder for contractors to respond to the umbrella consultation (closing today) Aug 29 08:09
- Top 5 most in demand cyber security contract roles Aug 25 08:38
- Changes to the right to request flexible working are incoming, but how will contractors be affected? Aug 24 08:25
Leave a comment: