[65] Tutorial :: Writing a small webserver - posted: 2011-09-16 22:21:01In this tutorial I will write a simple web server, and i mean a very simple web server, i will take you step by step as i write it, allowing you to learn some of the language and at the same time end up with your own small simple web server to start out with. It is expected you have experience with winsock and the windows API.

ok first we need a basic ziron project file with a few extra includes, such as the winsock file and console.

//this const will be sent before sending content type
const HTTP_HEADER = 'HTTP/1.1 200 OK\nContent-Type: ';
//this variable will hold the number of bytes received from recv
DWord bytesReceived;
//this will be our buffer for incoming data
char inBuffer[65537];
//a handle for the file being output to the client
DWord hFile;
//this buffer will be for the file that is read from disk
char IndexPage[65535];
//this buffer is for our file name and path.
char fileNameBuf[256];

ok in our loop where the process comment is we will place some more code

Code:

//receive some data
bytesReceived = recv(clientsocket, @inBuffer, 65536, 0);
ebx = @inBuffer;
//add a null char at the end for safety
char[ebx+eax] = 0;
//if no bytes were incoming, do nothing
if (bytesReceived != 0) {
//if the incoming data is a GET request we will process it
if (char*[ebx] == ord('GET ')) {
//call a process function we will write in a moment.
processInBuffer();
//try to open the requested file that our process function will get for us.
hFile = fileopen(@fileNameBuf);
//if the file exists and is ok we will output it
if (hFile != INVALID_HANDLE_VALUE) {
//we will reuse the bytesReceived instead of creating a new variable.
ReadFile(hFile, @IndexPage, 65535, bytesReceived, nil);
send(clientsocket, @IndexPage, bytesReceived, 0);
fileclose(hFile);
}
}
char[ebx] = 0;
}

i will leave this function for you to study, if you have questions, please do reply.
well, that is it, we have build a tiny web server, bare in mind it has no security and many other features are missing that you can add, maybe in later tutorials i can extend this web server.

[68] - posted: 2011-09-17 18:27:36made a few extra updates to the code, if you have questions about it, just ask, it still has some major things needed, for example multitasking...right now only 1 user actually downloads a file at a time.