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
Contributors
User avatar
vbstuff
New Member
New Member
Posts: 19
Joined: Wed Feb 02, 2011 7:49 pm

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:
Image

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

2.Change the caption of the buttons:

Image

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

Drag it onto the form:
Image

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

Image

NOW FOR THE CODE

5.Click on 'Start' and type:
Code: Select all
TCPServer1.Open;
  Memo1.Lines.Add(DateTimeToStr(Now)+':server started');
What this will do is start the server and put some text in the memobox

6.next copy this code:
Code: Select all
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;
Basicly this is the bit that makes the server work. it gets the files and checks for errors.

7.New go back to the form and double click on the 'Stop' button and type this in
Code: Select all
TCPServer1.Close;
  Memo1.Lines.Add(DateTimeToStr(Now)+':server stopped');
This basicly does the opposite to the start button, it stops the server

8.Last of all you need to click on the close button and type this
Code: Select all
close;
The code is pretty much self explanatory.

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:

Image

10. Open up notepad and type this in:
Code: Select all
<!DOCTYPE html>
<html>
<head>
<title>Test App</title>
<meta charset="UTF-8">
</head>
<body>
<h1>Hello World</h1>
<b>It Works:)</b>
</body>
</html>
of course this would be the code for the homepage code for your website but this will do for an example

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

Image

Running

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


Image

This is the code you should end up with:
Code: Select all
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;
__The 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.
User avatar
Shim
VIP - Donator
VIP - Donator
Posts: 882
Joined: Wed Dec 14, 2011 5:02 am

the tutorial is well explained !! :D , but i dont have delphi dunnno;
Find my programs on Softpedia
Vikhedgehog
VIP - Donator
VIP - Donator
Posts: 812
Joined: Fri Nov 05, 2010 6:24 pm

Lol cool, I'm thinking bout trying out delphi.
3 posts Page 1 of 1
Return to “Misc”