Winsock multi-program Program Control

Source: Internet
Author: User
Tags failover

Socket operations in Delphi
Delphi uses the tclientsocket component and the tserversocket component to operate and communicate with the slave socket and the socket of the server segment. The root socket connection method and the target of the local socket to be connected. The interface between the socket can be divided: use the remote connection, listener connection, and server connection.
(1) using a remote connection refers to the request for sending a response using the socket at the remote end. The target of the request is the socket at the server end. Therefore, to use a socket on the terminal, first describe the socket on the server to be connected, mainly the address and port number of the socket on the server, then locate the server socket to be connected. After finding the result, request a response from the server socket. At this time, the socket on the server may not be exactly the same as when the server is busy. However, the server socket will automatically upload a client. Please request the parameter column. This parameter column is sorted first, during the specified period, an accept message will be sent to the client socket by requesting a response, in this way, the connection is established between the client and the server through sockets!
(2) When a listener is connected, the socket on the server end does not locate the socket on the receiver end, but waits for the response to be received, when the server socket listener hears or receives the Response Request from the receiver socket, it will apply to the remote socket. Please create a new socket control interface and connect it with the remote client. The server socket cannot be accessed directly, in this way, you can establish a connection with multiple clients at the same time.
(3) The received server-side connection means that when the server-side socket receives a Response Request from the terminal-side socket, the description of the socket on the server is sent to the receiver. Once the description is confirmed by the terminal, A failover is established!
3 proposal of the Program Control System
Once the server is connected to the client, you can access the Internet to collect information and resources. However, in WinSock, there are two zookeeper modes: "blocking" and "non-blocking.
Generally, refer is not blocked. If the clienttype attribute is set to ctnonblocking, The clienttype indicates that the clienttype is connected in non-blocking mode. When the socket on the server is used to initiate or terminate operations, the onread or onwrite event will be notified when the socket on the server is used.
For the server socket, if the servertype attribute is set to stnonblocking, it indicates that the slave node is connected in non-blocking mode. When the client socket is used to initiate or terminate a transaction, the server socket will be notified, that is, the onclientread or onclientwrite event.
Unlike non-blocking, there are no non-synchronous events such as onread or onwrite in blocking mode. The socket must be subject to the upload or upload data. Before the zookeeper operation is completed, other generations cannot write a line. As a pure example, the entire application is waiting for failover, greatly reduces the performance of applications.
For example, if the clienttype attribute is set to ctblocking, The clienttype indicates that the clienttype is blocked for access, in order to reduce the possibility of blocking operations, you can put all operations related to batch operations in a simple process, in this way, other threads can be used to obtain rows.
For the server socket, if you set servertype to stthreadblocking, it indicates that the slave node is blocked and connected. In Delphi, a new route is automatically assigned for each blocking method, even if a customer is performing a rolling operation, other customers do not have to wait.
4. Use multi-programming on the slave end
In blocking mode, to reduce the possible side effects of blocking mode, you can put all the operations involving blocking operations in a single program. In this case, you need to create a new program object, and then re-execute its execute method, we perform the zookeeper operation through the twinsockstream object.
Procedure tclientthread. Execute;
VaR sstream: twinsockstream;
Sbuffer: string;
Begin
// Create a twinsocketstream object example.
Ssteam: = twinsockstream. Create (clientsocket. socket, 60000 );
Try // obtain and operate commands until the slave node is connected to the slave node or the master node is stopped.
While (not terminate) and (clientsocket. Active) Do
Begin
Try
Getnextrequest (sbuffer );
// Request a response to the server
Sstream. Write (sbuffer, length (sbuffer) + 1 );
...
Except
If not (snapshot t object is eabort) then
// Handle custom situations
Synchronize (handlethreadexception );
End;
End;
Finally
Sstream. Free;
End;
End;
5. Use multi-programming on the server side
On the server side, Delphi assigns a new route for each blocked route and runs each route through the tserverclientthread. Therefore, you cannot create a route object by using the route parameter in the object catalog. You can only manually create a derived class of tserverclientthread, and then re-compile the clientexcute method. Procedure tserverthread. clientexcute;
VaR sstream: twinsocketstream;
Sbuffer: array [0 .. 9] of char
Begin
// Obtain and operate commands until the slave node is connected to the slave node or the master node is stopped.
While (not terminate) and (clientsocket. Active) Do
Begin
Try
Sstream: = twinsocketstream. Create (clientsocket. socket, 60000 );
Try // fill sbuffer into column
Fillchar (sbuffer, 10, 0 );
// Delay time 60 seconds
If sstream. waitfordata (60000) then
Begin
If sstream. Read (sbuffer, 10) = 0 then
Clientsocket. close;
......
End
Else clientsocket. close;
Except
Handleexception;
End;
Finally
Sstream. Free;
End;
End;
End;
Conclusion: It is especially convenient when we need to process large amounts of data through multi-platform programming on the slave end and server end, in addition, it can greatly improve the utilization of network resources. Currently, we are studying how to optimize the processing and sending of data through the history program control system!

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.