Fast-forward with Indy Misc components

Posted: (EET/GMT+2)

 

Fast-forward with Indy Misc components

Apr 6, 2001

Borland Delphi 5 Professional or Enterprise
Indy Components 8.0.21

If you have used Delphi for Internet programming, you have probably learned what Indy components are. Also called WinShoes 8, Indy components allow you to write TCP/IP or UDP/IP applications very easily. The Indy components set includes both client and server components so that you could quickly write your own HTTP server if the need arises. Since the Indy demos leave the "misc" components into your own study, this article provides a quick introduction to their use.

Indy components, or Internet Direct more precisely, are an evolution of WinShoes 7. These components are open-source and can be downloaded freely from www.nevrona.com/indy. After you have installed the components to you version of Delphi, the components appear similarly to the following:

indy misc components

As you can see, Indy components occupy three tabs from the component palette, so you should have a large monitor to use all those components without scrolling. If you are new to socket programming, you should probably read the introductory topics in the Indy pages.

However this document assumes you are familiar with TCP/IP and related techniques. The sample application demonstrated in this article looks like this:

indy misc demo

The idea of this application is to provide you hints and tips how to get started with four components:

  • TIdDateTimeStamp
  • TIdNetworkCalculator
  • TIdCoderMD5
  • TIdBase64Encoder

Even though the code lines are sparse, you will get an understanding about how these components work.

Calculating a Date

The first components we're introducing is TidDateTimeStamp. This component is a very simple component: it allows you to retrieve dates and times in the given format. The available formats are RFC 822 (e-mail format) and ISO 8601. To get a datetime in either format, use the As* properties. The following code shows how to use this component:

procedure TForm1.Button1Click(Sender: TObject);
begin
  IdDateTimeStamp1.SetFromTDateTime(Now);
  IdDateTimeStamp1.TimeZone := +2;
  RFC822.Text := IdDateTimeStamp1.AsRFC822;
end;

Note that TimeZone should be set according to Windows regional settings, but this is beyond the scope of this article. So, the example code will function correctly in Helsinki, Athens and Istanbul for instance (GMT +2).

The second component is TIdNetworkCalculator. This component helps you calculate network subnet masks and IP addresses (IPv4, unfortunately the component doesn't support IPv6). For example, assume you have an IP address like 213.169.1.17. To break up this address to its byte-sized parts, use the following code:

procedure TForm1.Button2Click(Sender: TObject);
begin
  IdNetworkCalculator1.NetworkAddress.AsString := IPAddr.Text;
  ShowMessage('Components of IP address are:'#13+
              IntToStr(IdNetworkCalculator1.NetworkAddress.Byte1)+#13+
              IntToStr(IdNetworkCalculator1.NetworkAddress.Byte2)+#13+
              IntToStr(IdNetworkCalculator1.NetworkAddress.Byte3)+#13+
              IntToStr(IdNetworkCalculator1.NetworkAddress.Byte4));
end;

Of course, the string "#13" above is the ASCII code for a carriage return (CR).

Coding data

In addition to general-purpose helper components, the Indy Misc component palette tab contains important components for dealing with data coded in different formats. For example, you can use these components to encode and decode Base64 and Unix-to-Unix data (UU).

The principle of all these coder components is the same: you feed data to them using a method such as CodeString, and the finally read the encoded or decoded result using the CompletedInput function. If you are using small pieces of data, you can enable AutoCompletion, which means that the CodeString function will immediately return the encoded/decoded string.

The following code shows these two ways to use the components:

procedure TForm1.Button3Click(Sender: TObject);
begin
  If MD5.Checked Then Begin
    IdCoderMD51.Reset;
    IdCoderMD51.AutoCompleteInput := True;
    Coded.Text := MD5ToString(Strip(IdCoderMD51.CodeString(Source.Text)));
  End
  Else Begin
    IdBase64Encoder1.Reset;
    IdBase64Encoder1.CodeString(Source.Text);
    Coded.Text := Strip(IdBase64Encoder1.CompletedInput);
  End;
end;

Note that unfortunately the TIdCoderMD5 doesn't work correctly (our example uses version 8.0.21). When fed something else than a null string, it returns an erroneous result, at least according to RFC 1321. Also, when using the coder components, remember that you need to reset the components after using them. Do this by calling the Reset method.

Download the example code

Download fastforwardwithindymisc.zip (194 kB) which contains the sample application Indy Misc Demo. Please note that the sample application will require Delphi 5 Professional or Enterprise as well as Indy Components 8.0.21 or later.

* * *

Need help developing socket applications with Delphi? Contact us!