Using the Command Handler Method in Delphi - Indy clients
(Page 4 of 5 )
Clients are very simple to implement, because they basically just send a request and process the response. Dropping an idTCPClient component on a form with a button, an edit box and a memo is basically all it takes to create a client. Once you connect the client to a server, all you do then is send a command and wait for a response from the server, which you display in the memo.
From our previous article we know that clients need a hostname or IP address and port number to connect to a server. So connecting to a client takes the form:
with IndyClient do begin
Host := 'myhostname.com' ; // You can use an IP Address here as well.
Port := 6000; //This is where the server is listening
Connect;
Try
// Read and write data here
finally
Disconnect;
end;
end;
The code above shows the minimum requirements for creating a client application:
- Set host property.
- Set port property.
- Connect.
- Reading/Writing.
- Disconnect.
We also need to consider exception handling. In most cases when using client-server applications, the server application will be located at a remote place. Since you don't know if the server is running or not, you should make provisions in your code for any exceptions that may result from the server being inactive. All indy exceptions are descended from EIdException:
try
IdTCPClient1
try
// read/write here
finally IdTCPClient1
except
on E: EIdException do begin
ShowMessage('Indy Exception: ' + E.Message);
end else begin
ShowMessage('VCL Exception: ' + E.Message);
end;
end;
Next: Building a More Appealing Client >>
More Delphi-Kylix Articles
More By Leidago