How to write a Client (Delphi)

This article explains how to create a Remoting SDK for Delphi client that connects to and uses the services of the server that we created in the How to write a Remoting SDK Server (Delphi) article. It takes the form of a tutorial and you will find a link to the completed source files at the bottom of the article.

Notes:

Setting up the client project and the main form

  • Start a regular Delphi application by selecting the File -> New -> Application menu item and save it in the same directory as our server project as MyROClient.dpr. Also, rename the new unit to fClientForm.pas.
  • Rename the form "Form1" form to ClientForm.
  • Add the TestLibrary_Intf.pas unit to the project via Project -> Add to Project.

Tip: an alternative method to add the service interface to the project is by using the Import Remoting SDK service command from the Tools -> Remoting SDK menu. You will be presented with two options, as shown below:

  • Finally, select ClientForm in the IDE and add the TestLibrary_Intf.pas unit to its uses clause via the File -> Use menu item.

The implementation section of fClientForm.pas should now look like this:

implementation

uses
  NewLibrary_Intf;

{$R *.dfm}
  
end.

Adding the Remoting SDK client components

The server we created to respond to HTTP requests was encoded using the Remoting SDK binary format. For the client to communicate with it, we need an HTTP client and a binary message encoder.

  • Select the Standard tab in your component palette and drop two TButtons on the form. Change their captions to Sum and GetServerTime.
  • Select the Remoting SDK tab in your component palette and drop a TROWinInetHTTPChannel and a TROBinMessage on the form.
  • Set ROWinInetHTTPChannel1.TargetURL's value to http://localhost:8099/Bin.

Note: the channel and message settings chosen should match what you set for the server. If your service is being called by non-RO clients, you will have to change the Message Class to use SOAP, XML-RPC or PostMessage (the default is our efficient BinMessage).

This is how your form should look once you're done:

Accessing the server

It's now time to enter some code. Double click on the button with the Sum caption and enter the following code:

procedure TClientForm.Button1Click(Sender: TObject);
var
  myservice: INewService;
  res: integer;
begin
  myservice := CoNewService.Create(ROBINMessage1, ROWinInetHTTPChannel1);
  res := myservice.Sum(1, 2);
  ShowMessage('The result is '+IntToStr(res));
end;

Double click on the button with the GetServerTime caption and enter the following code:

procedure TClientForm.Button2Click(Sender: TObject);
var
  myservice : INewService;
  res : TDateTime;
begin
  myservice := CoNewService.Create(ROBINMessage1, ROWinInetHTTPChannel1);
  res := myservice.GetServerTime;
  ShowMessage( 'The time on the server is '+DateTimeToStr(res));
end;

We are done! We can now test the client.

To do this, launch the server executable using Windows Explorer and then click F9 from inside the Delphi IDE to compile and run the client. This is what you will see when you press the Sum button:

Conclusions

As you can see, the Remoting SDK makes creating remote services and clients a snap and allows you to concentrate more on your business logic.

Everything that had to do with HTTP connectivity and message encoding was done for you by the SDK, and accessing a remote object is now as simple as accessing regular Delphi objects.