How to Use Delphi to design powerful server programs

Source: Internet
Author: User

Nowadays, with the popularity of the network, server programs are widely used. How can we use Delphi to design strong servers?

Some people say that if you want to design a server, you must use VC to design it. In fact, this person makes some sense, because if you want to design a server using Delphi, if you want to design an efficient server, do not use most of the controls that Delphi brings (it is best not to use the Delphi control). Why? I will tell you the following. In this way, you can use APIs to design servers, which is no big difference with VC.

Use Delphi to design the server program. The specific choice is to use the form message mode or the port mode, which depends on the number of user connections. If the number of user connections is less than 1000, and the amount of data processed is not large, you can use the form message mode for server development. If the number of user connections is greater than 1000, in this way, it is best to use the complete port to develop the server. I suggest you use the complete port mode, because you cannot ensure that the number of your users does not change, and if your server is running for a while, it is best to make a WIN service program, which can ensure less maintenance in the future.

Now we will introduce the points you need to pay attention to when developing the Delphi Server:

1. Do not use the String variable in the program.

This is also found in the actual development process. When I first started the development, in order to be simpler, I used a large number of String variables to develop programs, however, the program is always running for a period of time and the cause is not clear. I checked the information on the Internet and found someone suggested not to use String as a variable, the problem of modifying all your programs into arrays is basically solved.

2. Use a fast encryption algorithm, such as XOR or DES encryption.

The server must encrypt the data transmitted between the client and the server, but what type of encryption algorithm is used? Do not use algorithms that require a large number of operations, such as RSA. It is best to use XOR or DES transposition encryption algorithms to meet common requirements for encrypted ciphertext, it also ensures the server's computing speed. You can also use RSA to encrypt the ciphertext, but this will cause server processing to slow down. If you encounter a large amount of processing, it is easy for the server to reject the server.

3. Use the original ADO function to connect to the database

Server programs are usually combined with databases, so the ADO control is usually used during Delphi development. However, if you study the ADO manual, you will find that, the server does not need controls to complete data operations. You can directly use the corresponding ADO function. This is mainly because the server programs and databases are usually relatively simple operations and are not very complex. So you can use the original ADO mode. This also reduces the problems caused by the ADO control.

4. Use more "pools"
The server must support a large number of variables during the design process. If you do not use the pool concept, your program will waste a lot of time in the process of creating and releasing variables. It is also prone to problems. Do not create or release variables during the design process. If you can consider the variables, they are all created at the beginning of the operation. This can speed up program running and reduce conflicts. I will discuss how to use the pool technology in the future.

5. proficient in Pointer operations
If you are not familiar with pointer operations, you can hardly design an efficient server. If you really want to understand the concept of pointer, it will be even more powerful for designing servers.
The following is an example. For example, after receiving data to the Buffer using Recv, You need to perform decryption. You can use 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); // use DES for encryption and decryption.
Move (B, ResultBuffer [(I-1) * 8 + 1], 8 );
End;
End
Let's take a look at the above Code. The idea is very clear, that is, the received Buffer is divided into eight mentioned variable a, and then the DES decryption algorithm is used to decrypt the received Buffer into B, and then put it back in ResultBuffer.
If you are familiar with 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); // use DES for encryption and decryption.
End;
End
Let's take a look at the above Code to see if two data Copy processes are missing. This is the efficiency that the pointer brings to you.

6. Use WinSocket 2 functions such as WSASend and WSARecv instead of Send and Recv functions.
This mainly depends on the system in which your server is running. If you are running in the WIN system, you 'd better use the WSA system functions, because Microsoft has optimized them all.

7. reasonably use the thread pool for operations
An efficient server must use the thread pool technology. The number of threads used is reasonable and the data to be processed by the thread is required. I personally think that if you want to use the thread pool technology, you must handle the most time-consuming operations, such as database query operations.

8 if the server uses the "pool" concept, there is another problem. How can we efficiently allocate the pool?
I use a large number of pools in programs, such as thread pools and data pools. How do I allocate a pool when data arrives? I will not tell you about it here. I will write another article about the pool later. Describes in detail how to use a pool. You can also consider it yourself.

9 use efficient string operation functions
Because the server must run a large number of strings, if you use the built-in functions of Delphi, it will be time-consuming, so we recommend that you use QStrings here. pas string operation function set, I believe it will be helpful to everyone.

10 optimize your SQL query statements
You can optimize SQL query statements to improve the running efficiency, and use stored procedures to improve the running efficiency. (You need to read the database content for this knowledge. We will not talk about how to optimize it here .)

The above is my practical experience, not all of which are correct. I hope you can help me. If there is a better method, you can also discuss it.

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.