Delphi Labs: DataSnap XE - Simple Calculator Service

By: Pawel Glowacki

Abstract: Step-by-step tutorial for building a simple calculator service and client application using Delphi XE and DataSnap

    Create Simple Calculator Server Application

The first step is to create a new DataSnap server application using “DataSnap Server” wizard.

Start Delphi XE.

Select in the main menu “File -> New -> Other” and from the “New Items” dialog double-click on the “DataSnap Server” icon in the “Delphi Projects -> DataSnap Server” category.

Hide image

Hide image
Click to see full-sized image

In the first tab keep the default DataSnap “Project type” which is “VCL Forms Application”.

Hide image
Click to see full-sized image

On the second tab keep all the default values. Later we are going to replace sample DataSnap server methods with our own implementation.

Hide image
Click to see full-sized image

On the third screen we keep the default value “211” for the TCP/IP Port. It is always a good idea to click on the “Test Port” to make sure that it is available.

Hide image
Click to see full-sized image

On the last tab we are going to keep default server methods ancestor class which is “TComponent”.

Click on “Finish” and the wizard should create a new project with three units.

Save the whole project by clicking on “File -> Save All”.

Create a new directory for all files in this lab – for example “C:\DataSnapLabs\SimpleCalc\”.

Save main application form as “FormServerUnit” and keep default names for all other units – typically “ServerContainerUnit1” and “ServerMethodsUnit1” – and save project as “SimpleCalcServer”.

At this stage you should see the following in the Delphi Project Manager:

Hide image

Open “ServerMethodsUnit1.pas” unit and implement the functionality of the simple calculator service. Replace sample methods added by the wizard (“EchoString” and “ReverseString”) with “Add”, “Subtract”, “Multiply” and “Divide” methods.

The source code of the “ServerMethodsUnit1” unit should look like the following:

unit ServerMethodsUnit1;

interface

uses
  Classes;

type
{$METHODINFO ON}
  TServerMethods1 = class(TComponent)
  private
    { Private declarations }
  public
    function Add(a, b: double): double;
    function Subtract(a, b: double): double;
    function Multiply(a, b: double): double;
    function Divide(a, b: double): double;
  end;
{$METHODINFO OFF}

implementation

{ TServerMethods1 }

function TServerMethods1.Add(a, b: double): double;
begin
  Result := a + b;
end;

function TServerMethods1.Subtract(a, b: double): double;
begin
  Result := a - b;
end;

function TServerMethods1.Multiply(a, b: double): double;
begin
  Result := a * b;
end;

function TServerMethods1.Divide(a, b: double): double;
begin
  Result := a / b;
end;

end.

Listing 1: ServerMethodsUnit1.pas.

Our server is now fully implemented. In order to develop the client application, the server has to be running.

Select “Run -> Run Without Debugging” to run the server project and minimize its window.

Do not shut down the server until the end of this lab exercise.

    Create Simple Calculator Client Application

Right-click on the project group inside the Project Manager and select “Add New Project”.

Hide image

From the “New Items” dialog select “VCL Forms Application” from “Delphi Projects” category.

Hide image

Click “OK”. A new project should be added to the existing project group.

“File -> Save All”.

Locate the folder where the server project has been saved and save there the main form unit of the client application as “FormClientUnit”, the new project as “SimpleCalcClient” and the project group as “SimpleCalcGrp”.

At this moment the Project Manager should look like this:

Hide image

Make sure that the client project is active and from the “New Items” dialog select “DataSnap Client Module”.

Hide image

Again we are going to keep all the default values in the wizard.

Hide image
Click to see full-sized image

On the first tab keep the “DataSnap server location” to “Local server”.

Hide image
Click to see full-sized image

Our DataSnap server is “stand alone” so keep the default value and click “Next”.

Hide image
Click to see full-sized image

Our server is using “TCP/IP” as the connection protocol, so keep the default selection.

Hide image
Click to see full-sized image

Click on “Test Connection” to verify that the server listens on the default port 211 and click “Finish”.

The wizard will add two units to the client project: “ClientClassesUnit1” and “ClientModuleUnit1”.

The last task in this lab is to implement the user interface of the client application.

Let’s start from “using” the “ClientModuleUnit1” unit in your client’s main form unit. Make sure that client form unit is open in the editor and select “File -> Use Unit”.

Now in your client form’s code you need to use methods exposed via “ClientModule1.ServerMethods1Client” property. These methods have the same names and signatures as methods available on the server.

Below is the implementation of the main client application form.

unit FormClientUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm3 = class(TForm)
    EditA: TEdit;
    ButtonAdd: TButton;
    EditB: TEdit;
    ButtonSub: TButton;
    ButtonMult: TButton;
    ButtonDiv: TButton;
    LabelResult: TLabel;
    procedure ButtonAddClick(Sender: TObject);
    procedure ButtonSubClick(Sender: TObject);
    procedure ButtonMultClick(Sender: TObject);
    procedure ButtonDivClick(Sender: TObject);
  private
    { Private declarations }
  public
    function GetA: double;
    function GetB: double;
    procedure ShowResult(aValue: double);
  end;

var
  Form3: TForm3;

implementation

uses ClientModuleUnit1;

{$R *.dfm}

function TForm3.GetA: double;
begin
  Result := StrToFloat(EditA.Text);
end;

function TForm3.GetB: double;
begin
  Result := StrToFloat(EditB.Text)
end;

procedure TForm3.ShowResult(aValue: double);
begin
  LabelResult.Caption := FloatToStr(aValue);
end;

procedure TForm3.ButtonDivClick(Sender: TObject);
begin
  ShowResult(ClientModule1.ServerMethods1Client.Divide(GetA, GetB));
end;

procedure TForm3.ButtonMultClick(Sender: TObject);
begin
  ShowResult(ClientModule1.ServerMethods1Client.Multiply(GetA, GetB));
end;

procedure TForm3.ButtonSubClick(Sender: TObject);
begin
  ShowResult(ClientModule1.ServerMethods1Client.Subtract(GetA, GetB));
end;

procedure TForm3.ButtonAddClick(Sender: TObject);
begin
  ShowResult(ClientModule1.ServerMethods1Client.Add(GetA, GetB));
end;

end.

Listing 2: Implementation of client’s main form.

My final client looks like this:

Hide image

That’s it!

It is really super easy to create DataSnap servers and clients with Delphi XEJ

    Summary

In this Delphi Lab we have used Delphi XE for building a simple calculator DataSnap XE service consisting of server and client native Win32 applications communicating with each other using TCP/IP protocol.

The full source code for this article is available from http://cc.embarcadero.com/Item/28184

The video version of steps described in this article can be found on YouTube (http://www.youtube.com/watch?v=Qm__RdmIWSg).

More information about Delphi can be found on the Delphi home page http://www.embarcadero.com/products/delphi

Server Response from: ETNASC04