In this article, the second part of a series, we will continue discussing how TCP/IP works in tandem with client server applications. Then, we are going to use this knowledge to implement the server part of an example client-server application with Delphi. We are also going to look at the various components that are available to create network applications.
Building a Server Application in Delphi - Building a Server application (Page 5 of 5 )
There are two ways to built a server application in Indy: command handlers and through the OnExecute event.
Command handlers are mostly suited to text-based protocols. They enable you to set up your own protocol and implement it. Most protocols are text-based and can be used with the command handlers format, but are not suited to protocols which have a binary command structure or no command structure. Below is an example command handler that implements a command called "gettxt":
For servers that have a binary command structure or no command structure, the OnExecute event should be used. The OnExecute event runs for as long as there is a live connection. Here's an example:
You can see in the code above that I do not check for a connection nor do I do any looping. Both are done by Indy automatically. Indy repeatedly calls this event until there is no connection. This can be caused by the client disconnecting, or a disconnect call as above.
Conclusion
I've read extensively on both Delphi and indy threading models, but have not covered it all here. So if you want to learn more about indy components and the like, a excellent source is the "Indy in Depth" ebook. It tells you everything about how indy components work and in what situations you can use them.
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.