Home arrow Delphi-Kylix arrow Page 5 - Using the Command Handler Method in Delphi
DELPHI-KYLIX

Using the Command Handler Method in Delphi


In the previous article, we discussed the various ways in which we can create indy servers. Picking up from where we left off, in this article we are going to build a server application using the CommandHandler method.

Author Info:
By: Leidago
Rating: 5 stars5 stars5 stars5 stars5 stars / 2
December 18, 2006
TABLE OF CONTENTS:
  1. · Using the Command Handler Method in Delphi
  2. · Implementing the Second and Third Commands
  3. · Setting the Greeting
  4. · Indy clients
  5. · Building a More Appealing Client

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

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.

blog comments powered by Disqus
DELPHI-KYLIX ARTICLES

- Loading an XML Document into the DOM
- Delphi Wrapper Classes and XML
- Delphi and the DOM
- Delphi and XML
- Internet Access: Client Service
- 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...

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 8 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials