Coding a http simple server.
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.
Look here
http://www.w3.org/Protocols/rfc2616/rfc2616.html
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.
http://www.w3.org/Protocols/rfc2616/rfc2616.html
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.
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; SOCKADDR_IN addr; // 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; SOCKADDR_IN connAddr; 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 sprintf( response, "HTTP/1.1 200 OK\r\nContent-Length: %d\r\nContent-Type: text/html\r\n\r\n%s", strlen(contents), contents ); // 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;}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement