How to use Delphi to design a powerful server program

Source: Internet
Author: User

Now the popularity of the network, so that the server program has been widely used, then we use Delphi how to design a strong server?

Some people say, if you want to design a server, you must use the VC to design, in fact, this person said there is a certain reason, because if you want to use Delphi to design the server, you want to design efficient server do not use Delphi to bring most of the control (preferably do not use the Delphi control), Why is it? I'll tell you next. In this way you use the API to design the server, there is no big difference with the VC.

Use the Delphi to design the server program, the choice is to use the form message mode or use the completion port mode, which depends on the number of user connections to determine. If your user connection is less than 1000 people, and the amount of data processed is small, you can use the form's message mode for server development, and if it is greater than 1000, it is better to use the completion port to develop the server. I recommend that you best use the completion port mode, because you can not guarantee that the number of users do not change, and because your server if you run a period of time without problems, it is best to make a win service program, so as to ensure less maintenance later.

Now introduce you in the development of Delphi server when you need to pay attention to the place:

1 do not use string variables in your program

This is also found in the actual development process, I started the development of the time, in order to be simple, a lot of use of string variables to develop the program, but the program always run a period of time after the problem, and later check the reason is not very clear, to the online search data, found that someone introduced do not use string to do variables, The problem is basically solved by modifying all of your own programs.

2 algorithms such as XOR encryption or DES encryption using fast encryption algorithms

The server must be encrypted when it is passed to the client, but what type of encryption algorithm is used? Do not use the algorithm that requires a lot of arithmetic, such as RSA algorithm, preferably using XOR encryption or DES transposition encryption algorithm, this is mainly to meet the requirements of ordinary cryptographic ciphertext, but also to ensure the operation of the server speed. You can also use RSA encryption, but this can cause server processing to slow down, and if you encounter a lot of processing time, it is easy for the server to reject the server.

3 using the original ADO function to connect to the database

Server programs usually want to combine with the database, so when using Delphi development, usually using ADO control to make, but if you learn the ADO manual will find that the server actually does not need the control to complete the operation of the data. You can do this directly using the corresponding function of ADO. The main reason is that server programs and databases are generally simple operations and are not very complex. So using the original ADO mode is possible. This also reduces the problem caused by the ADO control.

4 should use "Pool" more
Server in the design process, must be a large number of variable support, if you do not use the concept of pool, your program will create and release variables in the process of wasting a lot of time. and prone to problems. Try not to create and release variables as much as possible during the design process, if the variables you can take into account are created at the beginning of the run. This can speed up the program and reduce conflicts. Specifically how to use the pool this technology, there is time to consider writing an introduction.

5 skilled use of pointer operation
If you are not familiar with pointer manipulation, then you can hardly design an efficient server, if you want to really understand the concept of pointers, for the design of the server is a powerful.
For example, if you use recv to receive data in buffer, you need to decrypt it, and you can do it using the following method:
Var
A,b:array [1..8] of byte;
I:integer;
Resultbuffer:array [1..Max] of byte;
Begin
For I: = 1 to Sizeof (Buffer) Div 8 do
Begin
Move (buffer[(i-1) *8+1],a,8);
Des (a,b,true); This uses des plus decryption processing
Move (b,resultbuffer[(i-1) *8+1],8);
End
End
We have a look at the above code, the idea is very clear, is to receive the buffer by 8 refers to the variable a, and then use des decryption algorithm decrypted to B, and then put back into the resultbuffer.
If you are skilled in using pointers, the efficiency will be greatly improved.
Var
A,b:pbyte;
I:integer;
Resultbuffer:array [1..Max] of byte;
Begin
For I: = 1 to Sizeof (Buffer) Div 8 do
Begin
A: = @Buffer [(i-1) *8+1];
B: = @ResultBuffer [(i-1) *8+1]
Des (a^,b^,true); This uses des plus decryption processing
End
End
Take a look at the above code, is not the process of two less copy data, this is the pointer to you to bring efficiency.

6 more Use WinSocket 2 functions such as WSASEND,WSARECV, do not use the SEND,RECV function
This mainly depends on what system your server is running on, and if it is running on the win system, it is best to use the WSA system functions, because Microsoft has optimized them all.

7 Proper use of thread pool operations
Efficient servers must use thread pooling technology, how many threads are reasonable, and what data the thread needs to process. I personally think that if you want to use the thread pool technology, be sure to deal with the most time-consuming operations, such as database query operations.

8 If the server uses the concept of "pool", there is another problem, how to allocate the pool efficiently?
I use a lot of pool in the program, such as thread pool, data pool and so on. When the data arrives, how do you allocate the pool? Here will not tell you, and then write a special article about the pool. Detailed description of how to use the pool. You can think about it yourself.

9 using efficient string manipulation functions
Because the server must be a large number of strings to run, if the use of Delphi's function to operate, it is more time-consuming, so it is recommended that you use the Qstrings.pas string operation function set, I believe it will be helpful to everyone.

10 Optimizing your SQL query statements
You can optimize SQL query statements to improve operational efficiency, and on the other hand you can use stored procedures to increase operational efficiency. (This knowledge you need to look at the content of the database, here specifically how to optimize it does not say.) )

The above introduction is my practical experience, not necessarily all right, I hope everyone can help. If there is a better way, it can also be discussed.

Http://www.cnblogs.com/wxy8/archive/2010/11/17/1879529.html

How to use Delphi to design a powerful server program

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.