How to make a program like apache in delphi
Questions/Tutorials for any programming language not covered in other sections.
3 posts
Page 1 of 1
This code is not by me but i thought that its quite useful :P
Welcome :!:
In this tutorial i will go over how to make a program/server like apache. For thoughs who dont no what a server is a server is a program which you can host your website and files on.
For the tutorial i will be using Delphi7 but newer versions should work fine
Ok lets start. First of all start delphi
You should see something like this:

1.Resize the form and add 3 buttons and a memobox:

2.Change the caption of the buttons:

3.Go across the top toolbar until you find 'Internet' then click on 'TCPServer':

Drag it onto the form:

4.Click on the server component you draged on the form and change these settings:
LocalHost = localhost
LocalPort = 7777

NOW FOR THE CODE
5.Click on 'Start' and type:
6.next copy this code:
7.New go back to the form and double click on the 'Stop' button and type this in
8.Last of all you need to click on the close button and type this
The Files
This is the last step of the server and that is making the folders and HTML files:
9.Create a folder in the same folder as the EXE file and call it htdocs:

10. Open up notepad and type this in:
Most of you reading this will know how to do this but if not then this is how to save the file
11.The last step is to save the file in the htdocs folder. make sure to choose all files and save it index.html

Running
Run and press the start button.
Then load up a webbrowser and type 'http://localhost:7777'

This is the code you should end up with:
Thank you for reading this tutorial
If it doesn't work try disabling your antivirus. if it still doesn't work make a comment below about the problem
I am still working on TVID so i will post a update on how its going
Heres the source files(Note that this is note the same as the code above but its similar)
Welcome :!:
In this tutorial i will go over how to make a program/server like apache. For thoughs who dont no what a server is a server is a program which you can host your website and files on.
For the tutorial i will be using Delphi7 but newer versions should work fine
Ok lets start. First of all start delphi
You should see something like this:

1.Resize the form and add 3 buttons and a memobox:

2.Change the caption of the buttons:

3.Go across the top toolbar until you find 'Internet' then click on 'TCPServer':

Drag it onto the form:

4.Click on the server component you draged on the form and change these settings:
LocalHost = localhost
LocalPort = 7777

NOW FOR THE CODE
5.Click on 'Start' and type:
Code: Select all
What this will do is start the server and put some text in the memoboxTCPServer1.Open;
Memo1.Lines.Add(DateTimeToStr(Now)+':server started');
6.next copy this code:
Code: Select all
Basicly this is the bit that makes the server work. it gets the files and checks for errors.procedure TForm1.TcpServer1Accept(Sender: TObject;
ClientSocket: TCustomIpClient);
var
Line: string;
Path: string;
HTTPPos: integer;
begin
Line:= ' ';
while ClientSocket.Connected and (Line <> '') do
begin
Line := ClientSocket.Receiveln();
Memo1.Lines.Add(Line);
if Copy(Line,1,3) = 'GET' then
begin
HTTPPOS := Pos('HTTP',Line);
Path := Copy(Line,5,HTTPPos-6);
Memo1.Lines.Add('Path: '+ Path);
end;
end;
if Path = '/' then
Path := 'index.html'; //This bit can be changed to what you want your websites homepage to be
//htdocs can be changed to the folder your website is at
if FileExists('htdocs/' + Path) then
with TStringList.Create do
begin
LoadFromFile('htdocs/' + Path);
ClientSocket.Sendln('HTTP/1.0 200 OK');
ClientSocket.Sendln('');
ClientSocket.Sendln(Text);
ClientSocket.Close;
Free;
exit;
end;
ClientSocket.Sendln('HTTP/1.0 404 Not Found');
ClientSocket.Sendln('');
ClientSocket.Sendln('<h1>Error 404</h1><br>Path: '+Path); //This bit can be changed to what you want to display if the webpage cant be found.
ClientSocket.Close;
end;
7.New go back to the form and double click on the 'Stop' button and type this in
Code: Select all
This basicly does the opposite to the start button, it stops the serverTCPServer1.Close;
Memo1.Lines.Add(DateTimeToStr(Now)+':server stopped');
8.Last of all you need to click on the close button and type this
Code: Select all
The code is pretty much self explanatory.close;
The Files
This is the last step of the server and that is making the folders and HTML files:
9.Create a folder in the same folder as the EXE file and call it htdocs:

10. Open up notepad and type this in:
Code: Select all
of course this would be the code for the homepage code for your website but this will do for an example <!DOCTYPE html>
<html>
<head>
<title>Test App</title>
<meta charset="UTF-8">
</head>
<body>
<h1>Hello World</h1>
<b>It Works:)</b>
</body>
</html>
Most of you reading this will know how to do this but if not then this is how to save the file
11.The last step is to save the file in the htdocs folder. make sure to choose all files and save it index.html

Running
Run and press the start button.
Then load up a webbrowser and type 'http://localhost:7777'

This is the code you should end up with:
Code: Select all
__The End__procedure TForm1.Button1Click(Sender: TObject);
begin
TCPServer1.Open;
Memo1.Lines.Add(DateTimeToStr(Now)+':server started');
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
TCPServer1.Close;
Memo1.Lines.Add(DateTimeToStr(Now)+':server stopped');
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
Button2Click(Sender);
close;
end;
procedure TForm1.TcpServer1Accept(Sender: TObject;
ClientSocket: TCustomIpClient);
var
Line: string;
Path: string;
HTTPPos: integer;
begin
Line:= ' ';
while ClientSocket.Connected and (Line <> '') do
begin
Line := ClientSocket.Receiveln();
Memo1.Lines.Add(Line);
if Copy(Line,1,3) = 'GET' then
begin
HTTPPOS := Pos('HTTP',Line);
Path := Copy(Line,5,HTTPPos-6);
Memo1.Lines.Add('Path: '+ Path);
end;
end;
if Path = '/' then
Path := 'index.html';
if FileExists('htdocs/' + Path) then
with TStringList.Create do
begin
LoadFromFile('htdocs/' + Path);
ClientSocket.Sendln('HTTP/1.0 200 OK');
ClientSocket.Sendln('');
ClientSocket.Sendln(Text);
ClientSocket.Close;
Free;
exit;
end;
ClientSocket.Sendln('HTTP/1.0 404 Not Found');
ClientSocket.Sendln('');
ClientSocket.Sendln('<h1>Error 404</h1><br>Path: '+Path);
ClientSocket.Close;
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
form2.Show;
end;
Thank you for reading this tutorial

If it doesn't work try disabling your antivirus. if it still doesn't work make a comment below about the problem
I am still working on TVID so i will post a update on how its going

Heres the source files(Note that this is note the same as the code above but its similar)
You do not have the required permissions to view the files attached to this post.
Lol cool, I'm thinking bout trying out delphi.
3 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023