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. |