Indy Step by Step - Part 2.1Overviw of TIdTcpServer Component(5分)

A

aq13

Unregistered / Unconfirmed
GUEST, unregistred user!
Maybe you think we are starting now with the hard part. In fact this is true. We are starting with the hard part of indy. And, when we will build the client component, you will be amazed to know how easy is to build a client with indy.
For now, open a new project and put an IdTcpServer component on the form. IdTcpServer Component is located on the Indy Server tab (it is the first component in this tab). Now, set the following properties for this component:
Property Name Property Value
Active True
DefaultPort 1111
Greeting
Text Welcome to my server!

In fact, your first TCP server is almost complete. You haven't write any line of code and you have a working server. Amazing, isn't it? Let's test it! Run your program. A blank window will appear. For testing TCP servers, the most convenient method is to use the telnet program. If this is something new to you, read the Windows help and you will know what telnet is. For now, using telnet start a connection to localhost at port 1111. In short time you will receive the following message:
Welcome to my server!

When the telnet program connects to your server (as you maybe know your server works on port 1111;
you have set up this property!) your server replays with the "Greeting message". A greeting is a special message that you receive when you are connected to a server. You can see greetings in real world. Just connect to a ftp port (telnet on port 21) and if is a greeting set, you will see it.
Now it's time to avance in our server example. We will implement our first command. Let's see how todo
it! First I will describe how our server wilol work. First, when we will send to our server the command "responde" our server will replay with "Ok, I have responded". This is our first command implementation. For this set for the property CommandHandlersEnabled the true value and then
do
uble click on the CommandHandlers property. A Collection Editor will be open. Here click on AddNew button and a new item will apper. Here, set in Object inspector the following properties:
Property Name Property Value
Command responde
ReplyNormal
Text Ok, I have responded!

Compile this project and test it with telnet program... After you connect, just enter the responde command and our server will reply with the "Ok, I have responded!" message. Amazing. All work wasdo
ne without any line of code. Thanks to indy!
Before we will continue, we have to look in depth on the indy server internal architecture. Is a good step. After that you will understand all the indy server aspects. You must know that, each descendant of TIDTcpServer has the following behavior. First, when a client connects to it, a listener thread "intercept" this connection and then
creates a thread that will handle all communication events. So, in the internal architecture there are 2 threads categories. First is a listener thread that "listen" and waits for a connection. When a client is connected, this thread transfer all the communication operations to another thread. This architecture is very cool because your client application will be able to connect any time, even if there are many parallel connections to the server. For this second category of thread you have 2 threading models. First is IdThreadDefault and second is IdThreadPool.
IdThreadDefault model is the standard threading model you are using in your applications. For each connection, a separate thread is created. When the connection is finished this thread is destroyed.
The second model, IdThreadPool, is designed for high performance servers. In a high "aglomerated" environments, creating and destroing threads can be a time consuming operation, so your server performance can be faulted by this operation. So, conform to this model, a number of threads you specify are created when your server starts. When a connection begin
s, a thread is allocated and when connection stops, the thread is not destroyed.
This is all Indy Thread Concepts. Simply and fast. Maybe you are now interested how can you select the thread model you wish. This is simply. On the "Indy Misc" tab, you have two components thats are designed for each thread model: TIdThreadMgrDefault and TIdThreadMgrPool. Drop one of this components on the form and then
you can associate it to the TIdTcpServer component by filling the ThreadMgr property of the TIdTcpServer. If youdo
n't have anything in this propery, the server threading model will be the default model. So, a TIdThreadMgrDefault will be created. If you want to use the pool thread model, you have to write in the PoolSize property of the TIdThreadMgrPool component the no. of threads that will be created when server starts.
 
``````````````````can you say chinese?
 

Similar threads

A
回复
0
查看
862
Andreas Hausladen
A
A
回复
0
查看
738
Andreas Hausladen
A
A
回复
0
查看
483
Andreas Hausladen
A
В
回复
0
查看
476
Все для Delphi ...
В
顶部