Delphi-Kylix
  Home arrow Delphi-Kylix arrow Page 2 - A Real World Client Server Application in ...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
DELPHI-KYLIX

A Real World Client Server Application in Delphi
By: Leidago
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 10
    2006-12-26

    Table of Contents:
  • A Real World Client Server Application in Delphi
  • The Client
  • The Server
  • The Complete Server Code

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    A Real World Client Server Application in Delphi - The Client


    (Page 2 of 4 )

    To make our client truly thread safe we need to solve the following two problems:

    1. Reading and processing the response from server.
    2. Displaying that information in a thread safe manner.

    Currently, to read a response from the server, the client does this:

    procedureTForm1.Button1Click(Sender: TObject);

    begin

     IdTCPClient1.SendCmd(edit1.text);

     memo1.Clear;

     memo1.lines.add(idtcpclient1.Socket.ReadLn);

    end; 

    which as we've stated earlier is not thread safe.  So, to read the responses from the server we will create a thread that reads those responses. That will be its sole purpose.  It will be derived from the VCL's TThread  class:

    TReadResponse= class(TThread)

            protected

                FConn: TIdTCPConnection;

                procedure Execute; override;

            public

                constructor Create(AConn: TIdTCPConnection);
    reintroduce;

            end;

             TWriteResponse = class(TIdSync)

            protected

                FMsg: String;

                procedure DoSynchronize; override;

            public

                constructor Create(const AResponse: String);

                class procedure AddResponse(const AResponse: String);

            end;

    There are two classes here, TReadResponse and TWriteResponse. TReadResponse contains one method and a variable. The variable will hold the connection status of the client, and the execute method is used to do the actual reading:

            while not Terminated and FConn.Connected do

            begin

                TWriteResponse.AddResponse (FConn.IOHandler.ReadLn);

            end;

    You always override the execute method when you use the VCL's TThread class. This enables you to make it do whatever you want it to do. In our case, we want it to read the response from the  server.

    The TWriteResponse class is primarily responsible for writing the response that the client gets from the server to the Delphi component, which in this case is the memo component:

    procedure TWriteResponse.DoSynchronize;

         begin

    form1. memo1.Lines.Add(FMsg);

    end;

    The FMsg variable is used to store the response from the server. And the DoSyncronize method is then responsible for writing that response to the memo component. The class itself is derived from the TIdSync class. TIdSync allows for synchronizations with the ability to pass parameters to the synchronized methods as well. TIdSync also allows for return values to be returned from the main thread.

    Before writing the implementations of these methods, add the following to your form's variable section:

    rr: TReadResponse = nil;

    Your complete client code should look something like this:

    unit client;

    interface

    uses

      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

      Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection,

      IdTCPClient,IdException,idContext,idSync;

    type

      TReadResponse = class(TThread)

            protected

                FConn: TIdTCPConnection;

                procedure Execute; override;

            public

                constructor Create(AConn: TIdTCPConnection); reintroduce;

            end;

      TWriteResponse = class(TIdSync)

            protected

                FMsg: String;

                procedure DoSynchronize; override;

            public

                constructor Create(const AResponse: String);

                class procedure AddResponse(const AResponse: String);

            end;

      TForm1 = class(TForm)

        IdTCPClient1: TIdTCPClient;

        Edit1: TEdit;

        Label1: TLabel;

        Memo1: TMemo;

        Button1: TButton;

        Label2: TLabel;

        Button2: TButton;

        Button3: TButton;

        Memo2: TMemo;

        Label3: TLabel;

        cname: TEdit;

        Label4: TLabel;

        procedure Button3Click(Sender: TObject);

        procedure Button2Click(Sender: TObject);

        procedure Button1Click(Sender: TObject);

        procedure IdTCPClient1Status(ASender: TObject;

          const AStatus: TIdStatus; const AStatusText: String);

        procedure IdTCPClient1Connected(Sender: TObject);

        procedure IdTCPClient1Disconnected(Sender: TObject);

      private

        { Private declarations }

      public

        { Public declarations }

      end;

    var

      Form1: TForm1;

      rr: TReadResponse = nil;

    implementation

    {$R *.dfm}

    constructor TWriteResponse.Create(const AResponse: String);

        begin

            FMsg := AResponse;

            inherited Create;

        end;

    procedure TWriteResponse.DoSynchronize;

         begin

         form1.memo1.Clear;

         form1.memo1.Lines.Add(FMsg);

    end;

        class procedure TWriteResponse.AddResponse (const AResponse: String);

        begin

            with Create(AResponse) do try

                Synchronize;

            finally

                Free;

            end;

        end;

     constructor TReadResponse.Create(AConn: TIdTCPConnection);

        begin

            FConn := AConn;

            inherited Create(False);

        end;

        procedure TReadResponse.Execute;

       

        begin

            while not Terminated and FConn.Connected do

            begin

                TWriteResponse.AddResponse(FConn.IOHandler.ReadLn);

            end;

        end;

        procedure TForm1.Button3Click(Sender: TObject);

    begin

    close;

    end;

    procedure TForm1.Button2Click(Sender: TObject);

    var

     E: Exception;

    begin

    IdTCPClient1.Host:='localhost';

    IdTCPClient1.Port:=6000;

    IdTCPClient1.Connect;

    try

    IdTCPClient1.Socket.WriteLn(cname.Text);

    except

    on E: EIdException do begin

    ShowMessage('Indy Exception: ' + E.Message);

    end else begin

    ShowMessage('VCL Exception: ' + E.Message);

    end;

    end;

    button2.Enabled:=false;

    end;

    procedure TForm1.Button1Click(Sender: TObject);

    begin

     IdTCPClient1.IOHandler.WriteLn(edit1.text+'@'+cname.Text+';');

     end;

    procedure TForm1.IdTCPClient1Status(ASender: TObject;

      const AStatus: TIdStatus; const AStatusText: String);

    begin

    memo2.Lines.Add(Astatustext)

    end;

    procedure TForm1.IdTCPClient1Connected(Sender: TObject);

    begin

      rr:= TReadResponse.Create(IdTCPClient1);

    end;

    procedure TForm1.IdTCPClient1Disconnected(Sender: TObject);

    begin

    if rr <> nil then

            begin

                rr.Terminate;

                rr.WaitFor;

                FreeAndNil(rr);

            end;

    end;

    end. 

    More Delphi-Kylix Articles
    More By Leidago


       · Client Server applications are used in almost all spheres of IT, and is very much a...
       · Leidago, Thanks for this tutorial, it works great and I have already implemented in...
     

    DELPHI-KYLIX ARTICLES

    - Constructing the Interface for an Internet A...
    - Building a Server Application for an Interne...
    - Building an Internet Access Control Applicat...
    - Client Dataset: Working with Data Packets an...
    - Using the Client Dataset in an N-Tiered Appl...
    - Using the Client Dataset in Two-Tiered Clien...
    - Using the Client Dataset in File-Based Archi...
    - Demystifying the Client Dataset
    - Working with INI Files in Delphi
    - Creating Data Link (UDL) Files in Delphi
    - Looking at the Details for an Invoicing Appl...
    - Invoicing in Delphi: Show Me the Money
    - Saving Images and Binary Files to a Database...
    - Saving Files to a Database using Delphi: Sav...
    - Creating CF Applications and Integrating a S...







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway