Delphi-Kylix
  Home arrow Delphi-Kylix arrow Page 5 - Using the Command Handler Method in Delphi
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
DELPHI-KYLIX

Using the Command Handler Method in Delphi
By: Leidago
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 2
    2006-12-18

    Table of Contents:
  • Using the Command Handler Method in Delphi
  • Implementing the Second and Third Commands
  • Setting the Greeting
  • Indy clients
  • Building a More Appealing Client

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Using the Command Handler Method in Delphi - Building a More Appealing Client


    (Page 5 of 5 )

    You can of course put in your own exception message. This is particularly useful when trying to connect to an inactive server. You can display a message that will be more informative to the user, so that he or she has some idea of what is going on when they cannot connect to a server.

    Previously we used telnet to access our server application. Telnet is good when you test your server applications on your local system but not very exciting. Let's built a client that is more appealing and that will let us put into practice all we learned about clients. Start a new application and add the IdTCPClient component, available from the indyclients tab, a edit box, two memo components and three buttons.  Arrange them so they look something like this:

    Click on the button called connect to server and add the following code:

    procedure TForm1.Button2Click(Sender: TObject);

    var

     E: Exception;

    begin

    IdTCPClient1.Host:='localhost';

    IdTCPClient1.Port:=6000;

    IdTCPClient1.Connect;

     try

    except

    on E: EIdException do begin

    ShowMessage('Indy Exception: ' + E.Message);

    end else begin

    ShowMessage('VCL Exception: ' + E.Message);

    end;

    end;

    button2.Enabled:=false;

    end;

    Then click on the button called "Send Command" and add the following code:

    procedure TForm1.Button1Click(Sender: TObject);

    begin

     IdTCPClient1.SendCmd(edit1.text);

     memo1.Clear;

     memo1.lines.add(idtcpclient1.Socket.ReadLn);

    end;

    Then click on the button called exit and add the following code:

    procedure TForm1.Button3Click(Sender: TObject);

    begin

    idtcpclient1.Disconnect;

    close;

    end;

    Now click on the idtcpclient component, then go to the events tab on the object inspector and double click on the Onstatus event. Add the following code:

    procedure TForm1.IdTCPClient1Status(ASender: TObject;

      const AStatus: TIdStatus; const AStatusText: String);

    begin

    memo2.Lines.Add(Astatustext)

    end;

    This code keeps you in the loop about what the client application is up to. It will often resolve the hostname to an IP address. Then it will give you the status of the connection.

    Here's a test run of the application:

    And here's what happens when I send a command that is not part of our protocol:

    Conclusion

    You can see for yourself how easy it is to implement a client server application using indy components. The most important thing to remember in client server applications is to make sure that the server application is running before you try to connect the client. The example we used here is fine when you don't expect to have a lot of clients connecting to your server. However, it is not thread safe and lacks a few other things that stop it from being a real world application. In the next article we will deal with the issue of writing real world client server applications as well as with the issue of writing thread safe applications.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · All comments are welcome, especially from those of you who write client server...
     

    DELPHI-KYLIX ARTICLES

    - Finishing the Client for an Internet Access ...
    - The Client for an Internet Access Control Ap...
    - User Management for an Internet Access Contr...
    - Important Procedures for an Internet Access ...
    - Server Code for an Internet Access Control A...
    - Constructing the Interface for an Internet A...
    - Building a Server Application for an Interne...
    - Building an Internet Access Control Applicat...
    - Client Dataset: Working with Data Packets an...
    - Using the Client Dataset in an N-Tiered Appl...
    - Using the Client Dataset in Two-Tiered Clien...
    - Using the Client Dataset in File-Based Archi...
    - Demystifying the Client Dataset
    - Working with INI Files in Delphi
    - Creating Data Link (UDL) Files in Delphi






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT