Internet Programming with Delphi.pdf

(990 KB) Pobierz
Inprise
Internet
Programming
with Delphi
Borland Delphi is known to be a great environment for the
development of stand-alone and client-server applications on the
Microsoft Windows platform. Its virtues range from full OOP
support to visual development, in a unique combination of
power and ease. However, the new frontier of development is
now Internet programming. What has Delphi got to offer in this
new context? Which are the features you can rely upon to build
great Internet applications with Delphi? That’s what this paper
intends to reveal. We’ll see that Delphi can be used:
by Marco Cantù
( http://www.marcocantu.com )
For direct socket and TCP/IP programming;
Table of Contents
In conjunction with third-party components that implement
the most common Internet protocols, on the client or the
server side;
The Challenges of Internet Programming ....................................... 1
To produce HTML pages on the server side, with the
WebBroker and Internet Express architectures;
As well as to work with Microsoft’s core technologies,
including MTS, COM, ASP, and ActiveX.
The Challenges of Internet
Programming
Internet programming poses new challenges to traditional
developer environments and to the programmers using them.
There are issues related with the implementation of standard
protocols, the use of multiple platforms (Microsoft Windows
accounts for most of the client computers on the Internet but
only a fraction of the servers), and the licensing schemes of
some RDBMS systems.
Delphi
56119174.001.png 56119174.002.png
Delphi
Most of the problems, however, relate with HTTP development:
Turning existing Windows applications into applications running
within a Web browser is more complex than it might seem at
first sight. The Web is stateless, the development of user
interfaces can be quite an issue, and you invariably have to
consider the incompatibilities of the most widespread browsers.
A new platform specifically aimed at areas of Internet
programming (typically the HTTP world) has emerged. These
environments favor server side development, often also allowing
the inclusion of database information within web pages. A
common solution is to write HTML pages with special
“scripting” tags included, which are going to be expanded by an
engine on the server. Interesting for small projects, most of
these technologies have limited scripting power, and force you to
mix HTML code and scripting code, and GUI scripting code
with database oriented code. On larger applications, this lack of
separation among the different areas of a program is considered
to be far from a good architecture.
Moreover, Microsoft’s DNA is going to be replaced by the new
Microsoft dotNET (or “.NET”) architecture – a new name and
approach that seems to imply that the previous architecture had
indeed serious limitations. DotNET is apparently going to be
more “open” and stresses a lot the importance of XML,
including pushing the support for the SOAP (Simple Object
Access Protocol) invocation protocol. Another key element of
dotNET is that COM is apparently going to be phased out (not a
nice idea for people who’ve invested in the approach Microsoft
was pushing yesterday).
Even with the advent of dotNet, Microsoft’s DNA architecture,
based on ASP for HTML scripting and COM/MTS/COM+ for
database manipulation, offers a higher perspective, but is limited
to the Windows platform and Microsoft’s own IIS Web server,
tends to work primarily with the Internet Explorer browser. The
current incarnation of DNA suffers from several limitations,
including DCOM unfriendliness with firewalls, complex
configuration and administration, some tie-in with Microsoft’s
technologies, databases included, and limited scalability. Also,
the overall architecture, with the separation of many layers
partial with status and partially stateless, seems to be still limited
for the challenges of the Internet.
Where does Delphi Fit?
With this rather complex situation going on, where does a
“traditional” development platform like Delphi fit? The goal of
Delphi in the Internet age is to bring the some power, flexibility,
ease of use, and database support.
The power of Delphi comes from the fully compiled code,
different from many script-based technologies, and from its
fully object-oriented architecture (which is not an after
thought, but has been the foundation of the language and
its libraries since version 1.0). Delphi natively compiled
applications are simple to deploy, as they are generally made
of a single self-contained executable code (with no extra
runtime libraries and custom component files). Actually,
dividing the single EXE in multiple packages is a useful
option that is offered by Delphi, which programmers can
fine-tune to choose the best deployment solution.
The flexibility of Delphi comes from a support not limited
to HTTP but open to most Internet protocols, as the
development environment allows you to write lower level
code to support new protocols, as the developers of many
native third-party components have done.
The ease of use of Delphi comes from the component-
based environment. Writing a mail client (eventually bound
to a web page) simply implies adding a couple of
components to your program, setting a few properties, and
writing very little code. Some of the samples found in
Delphi and the third party components are basically full-
featured email programs!
With the InternetExpress technology of Delphi 5, the ease-
of-use has been extended to allow the visual development
of HTML front ends based on data sets.
Database and client/server support has always been one of
the strongest features of Delphi and client/server
architectures remain the core of most Web applications and
Internet sites. Actually, if you’ve built your Delphi
2
Delphi
applications by separating the user interface from the back
end (typically using Data Modules for the latter) you are
ready to plug in a new user interface to your existing
“business rules” code.
This is particularly true for multi-tier MIDAS applications,
which separate the business logic and the user interface in
two separate applications, running on the client and
application server computers. Using the InternetExpress
technology, as we’ll see, you can simply build a new front
end for a Web server application, and make it available to
the client browsers.
Leveraging your existing Delphi code and allowing you to build
Windows and browsers based front end for the same core
application, are two key reasons to adopt Delphi as your Internet
development platform. But they are not the only reasons, as
other areas of Internet development are equally served by the
technologies included in Delphi.
Finally, with the forthcoming Kylix project (see
http://www.borland.com/linux), Borland are providing a
“Delphi for Linux”, allowing your server side applications to run
equally well on Microsoft Windows or Linux operating systems.
Delphi will be able to leverage features of the two platforms
without any tie-ins to a specific operating system, allowing your
Web server applications to run on the two most widespread
operating systems for Internet servers.
creates a closed system, in which other programs not
written by you cannot interact (which might be an
advantage or a disadvantage, depending on the situation).
That is, of course, unless you want to define a new protocol
and publish the specs for others to “join” you.
Figure 1: The Internet page of Delphi’s component palette,
hosting the socket and HTML producer components.
Implement an existing protocol on the client or on the
server side. This can be done again with the generic socket
components mentioned above, but its generally
accomplished by using protocol-specific Delphi
components provided by third parties, some of which are
even pre-installed in the Delphi IDE.
Support the HTTP protocol, the core of the Web, and the
HTML file format. As these play such a major role, I’ll
cover them separately from the other protocols.
The support for TCP/IP and socket programming in Delphi is
as powerful as using the direct API (Winsock, in case of the
Windows platform) but far simpler. The socket components, in
fact, shield the programmer from some of the complex technical
details, but surface the Windows handles and low-level APIs,
allowing for custom low-level calls.
Writing simple programs with socket support in plain C calling
the Windows APIs requires hundreds of lines of code, while
using the Delphi socket components, a few lines of code will
suffice, even for complex tasks. That’s the standard advantage of
component-based development. Also, building a simple user
interface for the program is often trivial in Delphi. With other
development environments, you need to program the socket in a
low-level language (such as C) and then write the user interface
with a different visual tool, integrating the two and requiring
knowledge of multiple languages.
Core TCP/IP Support
The common factor for all Internet and Intranet applications is
communication over TCP/IP sockets. Most of the time the
communication is constrained by a set of rules, known as a
communication protocol. For example, SMTP and POP3 are
two very simple protocols for sending and retrieving mail
messages, defined by the Internet standard bodies.
Using Delphi you can:
Implement the client and the server side of a proprietary
protocol, using the TServerSocket and TClientSocket
components, found in the Internet page of the component
palette. This is handy for distributed applications, but
3
56119174.003.png
Delphi
Client Side Protocols Support
To develop the client side of Internet applications, Delphi
provides you ready-to-use components. There are multiple sets
of native VCL components you can adopt, all based on a similar
philosophy:
object Mail: TNMSMTP
Host = 'mail.server.com' // your web
service
Port = 25
PostMessage.FromAddress =
'marco@marcocantu.com’
end
// code to send the above email
message
Mail.PostMessage.ToAddress.Add
( 'davidi@borland.com' );
PostMessage.Subject := 'Borland
Community Site' ;
PostMessage.Body.Add ( 'Hi David, I
wanted to ask you...' );
Mail.Connect;
Mail.SendMail;
Mail.Disconnect;
The NetMaster components are pre-installed in the Delphi
environment (see the FastNet page of the component
palette), and include client-side support for the most
common Internet protocols (including POP3, SMTP,
NNTP, FTP, and HTTP).
The Indy open source components (“Internet Direct”,
previously called WinShoes and now “federated” with the
Jedi Project) are available on Delphi 5’s Companion CD and
from their web site (http://www.nevrona.com/indy). It has
been announced that Indy will be included by default in
Delphi 6 and Kylix (Delphi IDE for the Linux platform).
In short, these are the advantages of using Delphi for supporting
Internet client applications:
Choice among various offerings of components (some of
which are totally free and open source)
The free ICS components (“Internet Component Suite”,
available at http://users.swing.be/francois.piette/icsuk.htm,
include the complete source code) and are designed and
maintained by Francois Piette and form another set of very
popular Delphi components, supporting most Internet
protocols.
Easy integration with existing applications
Easy development of new and specific user interfaces, with
Delphi visual and object oriented architecture
Server Side Protocols Support
Besides supporting web protocols in existing applications, or
writing custom client programs specifically for them (as a
completely custom email program), in a corporate environment
you often need to customize Internet server applications. Of
course, many of the available pre-built servers can be used and
customized, but at times you’ll need to provide something that
existing programs do not support.
In that case, you might think of writing your own server, if only
it wasn’t so complex. Using Delphi and a set of server side
components you can build custom servers with only limited
extra effort, compared to a client program, and achieve (or at
times exceed) the performance of professional quality Internet
server programs.
Server side components were pioneered by Jaadu
(http://www.jaadu.com), which offers a web server component
and are now available in the Indy component set (discussed
above). There is also a set of highly optimized native Delphi
A few other commercial offerings, including IP*Works
components (http://www.dev-soft.com/ipwdlp.htm) and
Turbo Power’s Internet Professional
(http://www.turbopower.com/products/IPRO/).
Some of these components map directly to their own WinSock
wrappers, others also use the WinInet library, a Microsoft system
DLL that implements support for the client side of FTP and
HTTP protocols. Regardless of the set of components you are
going to use, they are really quite simple to work with. If you
have an existing application, and want to mail-enable it, just drop
a couple of components onto your form (or data module), set
their properties (which include the indication of the mail server
you want to connect with) and write few lines of code.
For example, to send email with NetMaster’s component, you
can use the following simple code:
// component properties
4
Delphi
components, called DXSock (http://www.dxsock.com),
specifically aimed at the development of Internet server
programs. Some of the demonstrations of these component sets
are actually full-fledged HTTP, mail, and news servers.
by third parties, with the most well known HTML viewer
component being offered by David Baldwin (see the Web
site http://www.pbear.com).
The reverse of integrating a browser within your application, you
can integrate your application within the browser. This is rather
easy to accomplish by using Internet Explorer and the ActiveX
technology. Delphi supports this technology in full using the
ActiveForm technology. An ActiveForm is built in the same
visual way that a plain Delphi form is constructed, but an
ActiveForm is hosted within an HTML page of Internet
Explorer. You can even move existing programs to the Web by
hosting their main form within an ActiveForm.
Client Side Web Support
If many Internet protocols are important, and email is one of the
most commonly used Internet services, it is undeniable that the
Web (that is, the HTTP protocol) is the driving force of most of
the Internet development. Web support in Delphi is particularly
rich. Here, we are going to start by exploring the features
available on the client side (to integrate with existing browsers)
and then we’ll move to the server side, devoting plenty of time
to the Web server development that you can do with Delphi.
The HTTP components available in most suites allow you
to create a custom browser within your application: You can
reach existing Web sites and retrieve HTML files or post
custom queries. At this point you can send the HTML
content returned by the HTTP server to an existing browser
or integrate a custom HTML processor or HTML viewer
within your application. The ways in which can apply are as
follows:
Sending an HTML file to Microsoft’s Internet Explorer or
Netscape Navigator, either using it as an external
application, by calling the ShellExecute API function:
ShellExecute (Handle, 'open' ,
'c:\tmp\test.htm' , '' , '' ,
sw_ShowNormal);
Figure 2: An example of a wizard built with Delphi (it is
based on a PageControl component) and deployed within
an ActiveForm. The buttons allow you to reach different
pages of the form, without moving outside of the browser’s
page.
or integrating the Internet Explorer ActiveX control,
surfaced in Delphi as the ready-to-use WebBrowser
component.
Processing HTML in a custom way, to extract specific
information; this is useful in case you don’t need to show
the HTML to a user, but want to process it, eventually
extracting specific information from it.
This Microsoft specific technology (ActiveX is not supported by
other browsers) can simplify the deployment of simple Delphi
applications within an Intranet, as users can download the
programs they need by pointing their browser to specific pages.
The ActiveX technology, however, is not well suited for the
Internet, as too many people have different browsers or
operating systems, or disable this feature in their browser for
Showing the HTML within your program using a native
Delphi component (so that end users don’t need to have
Internet Explorer installed). These components are available
5
56119174.004.png
Zgłoś jeśli naruszono regulamin