For forums, blogs and more please visit our
Developer Tools Community.
By: Pawel Glowacki
Abstract: In DataSnap architecture you can use different communication protocols, like TCP/IP, HTTP or HTTPS, and you can also use transport filters that make it possible to programmatically access the raw stream of bytes that are transmitted between client and the server over the wire. Delphi XE comes with three DataSnap communication filters preinstalled: two for encryption and one for compression. In this lab we are going to build a simple system consisting of a server and a client application. Both applications will be standalone Delphi VCL Forms applications and they are going to communicate using TCP/IP as the transport protocol and use ZLib compression filters. The article for this demo is available at the https://edn.embarcadero.com/delphi and the source code at Embarcadero Code Central (https://cc.embarcadero.com/item/28232)
In DataSnap architecture you can use different communication protocols, like TCP/IP, HTTP or HTTPS, and you can also use transport filters that make it possible to programmatically access the raw stream of bytes that are transmitted between client and the server over the wire.
Delphi XE comes with three DataSnap communication filters preinstalled: two for encryption and one for compression.
In this lab we are going to build a simple system consisting of a server and a client application. Both applications will be standalone Delphi VCL Forms applications and they are going to communicate using TCP/IP as the transport protocol and use “ZLib” compression filters.
Click on the “File – New – Other” menu and inside the “New Items” dialog double-click on the “DataSnap Server” wizard in “Delphi Projects – DataSnap Server” category to create a standalone Delphi DataSnap server application.
In the first tab of the wizard keep the default project type “VCL Forms Application”.
Keep all the default values on the remaining wizard screens and click on “Finish” to generate the DataSnap server project.
Select “File – Save All” to save all the files in the new project generated by the wizard. In my case I am going to create a new “C:\DataSnapLabs\Filters\” folder, give the main unit of the project “FormServerUnit” name, keep default names for other units (typically “ServerMethodsUnit1” and “ServerContainerUnit1”) and name project “FiltersServer”.
Open the server container unit and select “DSTCPServerTransport1” component in the form designer.
Click on the ellipsis button for the “Filters” property. This property is a collection, so when you click on it, you should see the collection editor that makes it possible to add one or more filters to the transport component.
Click on “Add New” button in the collection editor (or just press “Insert” button) and select a desired filter in the dropdown for the “FilterId” property.
Delphi XE comes with three preinstalled filters: “PC1”, “RSA” and “ZLibCompression”.
You can add zero, one or more filters to the “Filters” property of a DataSnap transport component.
Add “ZLibCompression” filter to compress the traffic between your server and client.
Our DataSnap server with communication filter in place is now ready. It is time to implement the client.
Right-click on the project group node in the Project Manager and select “Add New Project”. Select “VCL Forms Application” from “Delphi Projects” category and “Save All”. It is best to save new files in the same folder as server project – in my case in “C:\DataSnapLabs\Filters\” folder. You can name the client’s main form unit “FormClientMain”, the whole project “FiltersClient” and project group: “DSFilters”.
Before we can implement the client, the server has to be running. Right-click on the project group and select “Build All”. Make sure that the server project is active (its name should be displayed in “bold” in Project Manager) and select “Run -> Run Without Debugging”. Minimize the server window. The server needs to be running during the client development.
At this stage our project group consists of a server and a client project. The server is running and we need to implement the client.
Open client application main form and drop a “TSQLConnection” component on the form.
Set its “Driver” property to “DataSnap” and “LoginPrompt” to “false”.
Try to set the “Connected” property to “true”.
It is not going to work like this. If should see the following error message:
We cannot connect to the server, because we have not enabled compression filter on the client.
Expand “Driver” property of the “SQLConnection1” component and add “ZLibCompression” to the “Filters” property of the DataSnap driver.
Now there is the most important part of this lab exercise, which is not obvious!
You need to add to the “uses” clause of any unit in the client application “DbxCompressionFilter” unit. The side-effect of this operation is that the compression filter is registered at the client in the “initialization” section of this unit.
Right-click on the “SQLConnection1” component and select “Generate DataSnap Client Classes”.
Save generated unit as “proxy”.
Add “proxy” unit to client form “uses” clause.
Add a button and an edit to the client form.
Double-click on the button and enter the following code in the “OnClick” event handler:
DBXCompressionFilter, // needed for "ZLibCompression" filter
procedure TForm3.Button1Click(Sender: TObject);
var aClient: TServerMethods1Client;
aClient := TServerMethods1Client.Create(SQLConnection1.DBXConnection);
Edit1.Text := aClient.ReverseString(Edit1.Text);
// needed for "ZLibCompression" filter
If you run the client application, you should be able to successfully call the “ReverseString” server method using compressed over-the-wire TCP/IP communication.
In this “Delphi Labs” episode we have looked into the DataSnap XE support for communication filters.
In this example we have built a test server and a client that communicates over the TCP/IP using “ZLibCompression” filter for compressing the raw byte stream exchanged between both applications.
The most important “gotcha” in this article, which is not obvious, was to add “DBXCompressionFilter” unit to the “uses” clause of any unit in the client project.
More information about DataSnap XE Filters can be found online on docwiki (https://docwiki.embarcadero.com/RADStudio/XE/en/Filtering_DataSnap_Byte_Stream), including the information how to encrypt the communication using two-phase communication protocol that allows public/private key exchange.
The source code for this article is available at the Code Central (https://cc.embarcadero.com/item/28232) and the video version of this demo is available on YouTube (https://www.youtube.com/watch?v=GE4FJDJI9RI).
More information about "Delphi Labs" is available at https://blogs.embarcadero.com/pawelglowacki.
Could not retrieve comments. Please try again later.
Download Delphi 10 now!
Webinars on demand!
More social media choices:
Delphi on Google+
@RADTools on Twitter
Server Response from: ETNASC04