1. Tech

Your suggestion is on its way!

An email with a link to:


was emailed to:

Thanks for sharing About.com with others!

Exchanging Data over the Network using Delphi - Part 2
An introduction to sending data over the network using Delphi and Indy. The article focuses on sending / receiving record data and raw (binary) data using TCP connections.
 More of this Feature
• Part 1: Sending text over TCP
 Join the Discussion
"Post your views, comments, questions and doubts to this article."
 Related Resources
• Indy knowledge base
• How to get your IP
• Communications Tools and Component
• Networking with Delphi
• Internet programming with Delphi
 From Other Guides
• Computer Networking: Socket Programming

In the first part of this article, I've presented you an example of sending textual data over the network using Delphi with TServerSocket and TClientSocket components. I've received several e-mail messages with Delphi developers asking for the next part of the article.
Here it is. But this time we'll be using Indy components. First why: in Delphi 7, TServerSocket and TClientSocket components we removed, only TTCPClient and TTCpServer can be found on the Internet page of the component palette. Second why....

Why Indy
"Internet Direct (Indy) is an open source internet component suite comprised of popular internet protocols written in Delphi and based on blocking sockets. Indy is included in Delphi 6+, Kylix 1+, and C++ Builder 6+. Both client and server implementations are included as well as full source code and comprehensive demos."

Ever since that first article about exchanging data over the network using Delphi, I've been primarily dealing with / researching this great component collection. You'll find Indy on 4 pages on the component palette: Indy Clients, Indy Servers, Indy I/O Handlers and Indy Misc - included in Delphi 6+. Whoa - more than 60 component expecially designed to support network programming with Delphi. If you have Delphi versions prior to Delphi 6 do not get desperate, Indy components can be freely downloaded and used.

I was so impressed with Indy, I've even created a new section on this site to support Indy with articles from various sources. I do strongly suggest you to take a look at Indy knowledge base - the page hosts several great introductory (and more expert) articles explaining TCP/IP, sockets and network programming in general.
One "problem" with Indy components is that there are NO demos available for you after you install Delphi (7) - if you want to get familiar with the concepts of programming using Indy, it is advisable to download demo projects - you'll find a link inside the Indy knowledge base. In the package you'll find more than 30 demo projects explaining how to use Indy from "simple" chat program, over a bit complex "how to send records" to more powerful "Image server" - a great example of sending images (that is, any binary data) over network.

To be able to understand all the key concepts behind programming with Indy and Delphi, I do suggest you to take a look at several articles listed under Indy knowledge base that explain TCP, (blocking) socket programming, IP's, hosts, ports, etc... You'll not be able to write any meaningful network-enabled code until you grasp all the information you can get. I'm not saying that you have to be a TCP guru to start using Indy components - it is just important to understand topics like IP, port, TCP and similar. There is no need, on the other hand, to fully understand what's happening with data packets when they travel through your local network or the Internet.

This is the most important part: in all client / server network applications, the main idea is to have the server look for commands from the client and than do something upon receiving the command. Note that, this does not mean, that server can, by default, send a command (data) to the client without having the client specifically asking for something.

In general, when using TIdTCPServer and TIdTCPClient components, in order to have "them" communicate, you'll need at least the following:

Server part (TIdTCPServer)

  • Specify Bindings property for the server - the IP and the Port where the Server listens for clients. An example:

    with IdTCPServer.Bindings.Add do

  • Write an OnExecute even handler - enabling the Server to reply to commands sent from the Client.
  • Or, use CommandHandlers and CommandHandlersEnabled to provide finer control over commands executed by a client connection.
  • Client part (TIdTCPClient)

  • Assign values to Host and Port properties
  • Connect to the Server
  • Send something
  • Disconnect from the Server
  • Or, in Delphi code, something like:

    with IdTCPClient do begin
      Host := '';
      Port := 9999;

    How to send records and how to send binary data
    I suppose you are now looking for some *real* code in this part of the article ;)
    To be honest my first idea was to pick one of the demo projects, for example the one you can find under "SendReceiveRecords" folder, and show/explain the code.
    On the second thought it seemed foolish to copy the code and paste it here to explain something you can find for yourself. The fact is that using Indy (TIdTCPServer / TIdTCPServer components) is quite, more or less, straight forward.

    Here's what I'm suggesting: please try that demo project and if you have questions of any kind, do not hesitate to ask them on our Forum. I'll do my best (and the rest of the Delphi developers there) to help you with any problems you might have, ok?

    Oh, ok: here's an example: ScreenThief - stealing screen shots over the Network

    As always I encourage you to post your views, comments, questions and doubts to this article on the Delphi Programming Forum. Discuss!

    Related Video
    Using Meta Data for Search

    ©2016 About.com. All rights reserved.