Jump to content
  • Advertisement
Sign in to follow this  

Coding a http simple server.

This topic is 4175 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Im gonna try to code a simple http server for the experience. Anyone know any usefull doc's on such a process besides the http proto spec's? I was thinking something in tutorial form, code sample, or just general guidelines. And by code samples i dont mean reviewing the massive apache source =P Preferebly in C++ or C# Thanks.

Share this post

Link to post
Share on other sites
Look here

Click on 14 Header Field Definitions for the meaning of the different header fields.
The easiest thing for this I think is to create a simple application that listens on port 80 and on connection it prints all incoming data to the screen. This way you connect with your browser to your own program and you will see the headers, so you see what you need to parse.
Then you write code to parse the header and send a reply.

You can also write a program that connects to some web-server on the net and sends the same headers as you got to your program, when connecting to it with your browser. Then let this program print the response, and you see how servers respond.

Share this post

Link to post
Share on other sites
Here's a small example to get you started if you need it. Should work to just run it and then type http://localhost/ in your web-browser.

#include <stdio.h>
#include <winsock.h>

int main() {
WSADATA wsaData;
int iRet;
SOCKET listenSocket;

// Start winsock
iRet = WSAStartup(0x202, &wsaData);
if(iRet != 0 || wsaData.wVersion != 0x202) {

// Create socket to listen
listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(listenSocket == INVALID_SOCKET) {

// Bind to port
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(80);

iRet = bind(listenSocket, (LPSOCKADDR)&addr, sizeof(addr));
if(iRet == SOCKET_ERROR) {

// Listen
iRet = listen(listenSocket, SOMAXCONN);
if(iRet == SOCKET_ERROR) {

// Loop and accept connections
while(true) {
SOCKET connSocket;
int connAddrLen;

connAddrLen = sizeof(connAddr);

// Accept connection
connSocket = accept(listenSocket, (LPSOCKADDR)&connAddr, &connAddrLen);
if(connSocket == INVALID_SOCKET) {
else {
char data[100];

// Print the address the connection is from
if(connAddrLen == sizeof(connAddr)) {
printf("Connection: %s : %d\n", inet_ntoa(connAddr.sin_addr), ntohs(connAddr.sin_port));

// Receive data
iRet = recv(connSocket, data, 100, 0);
if(iRet == 0) {
// connection closed
else if(iRet == SOCKET_ERROR) {
else {
char response[200];
char contents[100];

// Here you should really parse the received data, and receive
// more than 100 bytes, all the way until the client sends
// two \r\n in a row which means the header has ended
// then parse the header and respond accordingly

// Just assume that it's a GET header for now and respond with HTML

// Response body, that is the document you send back
sprintf(contents, "<html><body><h1>HTML Response</h1></body></html>");

// The response with header + contents
"HTTP/1.1 200 OK\r\nContent-Length: %d\r\nContent-Type: text/html\r\n\r\n%s",

// Send the data
iRet = send(connSocket, response, strlen(response), 0);
if(iRet == SOCKET_ERROR) {

// Close connection
iRet = closesocket(connSocket);

// Close listen socket
iRet = closesocket(listenSocket);

// Shutdown winsock
iRet = WSACleanup();

return 0;

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!