1 Introduction
The Windows Sockets Specification defines a set of network programming interfaces in Microsoft Windows based on the socket interface popular in U. C. Berkeley University bsd unix. It not only contains the familiar Berkeley socket library functions, but also contains a set of extended library functions for Windows Program Developers can make full use of the Windows message driver mechanism for programming.
The Windows Sockets specification is intended to provide application developers with a simple set of APIS, and to allow network software vendors to comply with them. In addition, on the basis of a specific version of Windows, Windows Sockets also defines a binary interface (ABI ), in this way, the application that uses the Windows Sockets API can work on the implementation of any network software vendor that complies with the Windows Sockets protocol. Therefore, this specification defines a set of library function calls and related semantics that can be used by application developers and implemented by network software vendors. We can use Winsock to transmit data and exchange information on the Internet, and we don't need to care about the details of network connections, so we are very popular with network programming programmers.
2. socket operations in Delphi
Delphi uses the tclientsocket component and the tserversocket component to manipulate the connection and communication between the client socket and the server socket. Based on the connection initiation method and the target of the local socket to be connected, the connections between sockets can be divided into client connection, listener connection, and server connection.
(1) Client connection refers to a connection request initiated by the client's socket. The target to be connected is the server's socket. Therefore, the client socket must first describe the server socket to be connected, mainly the server socket address and port number, and then locate the server socket to be connected. After finding the connection, request the connection from the server socket. At this time, the socket on the server may not be in the ready state. However, the server socket automatically maintains a customer request queue through which priority is given, the "allow connection" (accept) signal will be sent to the client socket through the request response method as appropriate, so that the connection is established between the client and the server through sockets!
(2) The so-called listening connection means that the socket on the server side is not located in the specific client socket, but is waiting for connection, when the server socket listens to or receives a connection request from the client socket, it establishes a new socket handle in response to the request from the client socket and connects it to the client, the server socket continues to be in the listening status, so that you can establish a connection with multiple clients at the same time.
(3) server-side connection refers to sending the description of the server-side socket to the client after the server-side socket receives the connection request from the client-side socket. Once the client confirms this description, a connection is established!
3. Proposal of thread control
Once the server establishes a connection with the client, data and files can be transmitted over the Internet. However, in WinSock, there are two transmission modes: "blocking" and "non-blocking.
Generally, non-blocking is used. On the client side, if the clienttype attribute is set to ctnonblocking, the connection is non-blocking. When the server socket attempts to perform read/write operations, the client socket will be notified, that is, the onread or onwrite event.
For server socket, if the servertype attribute is set to stnonblocking, the connection is established in non-blocking mode. When the client socket attempts to read/write, the server socket will be notified, that is, the onclientread or onclientwrite event.
Unlike non-blocking, there are no asynchronous events such as onread or onwrite in blocking mode. The socket must actively read or write data. Before the read and write operations are completed Code It becomes a pure exclusive method, and the entire application will be in the waiting state, greatly reducing the performance of the application.
For the client socket, if the clienttype feature is set to ctblocking, blocking is used for connection. to minimize the negative impact of blocking, all read/write operations can be placed in a separate thread, so that other threads can continue to be executed.
For server socket, if servertype is set to stthreadblocking, the connection is blocked. In Delphi, a new thread is automatically allocated for each blocked connection, so that other customers do not have to wait even if a customer is performing read/write operations.
4. Use multithreading technology on the client
In blocking mode, to minimize the side effects of blocking, you can put all the read/write operations involved in a separate thread. Therefore, we need to create a new thread object and reload its execute method. In the thread code, we use the twinsockstream object for read and write operations.
Procedure tclientthread. Execute;
VaR sstream: twinsockstream;
Sbuffer: string;
Begin
// Create a twinsocketstream object instance and set connection timeout
Ssteam: = twinsockstream. Create (clientsocket. socket, 60000 );
Try // obtain and operate commands until the connection is disconnected or the thread is terminated
While (not terminate) and (clientsocket. Active) Do
Begin
Try
Getnextrequest (sbuffer );
// Write the request back to the server
Sstream. Write (sbuffer, length (sbuffer) + 1 );
...
Except
If not (snapshot t object is eabort) then
// Handle custom exceptions
Synchronize (handlethreadexception );
End;
End;
Finally
Sstream. Free;
End;
End;
5. Use multithreading technology on the server side
On the server side, Delphi will automatically allocate a new thread for each blocked connection and manipulate each thread through tserverclientthread. Therefore, you cannot create a thread object through the wizard in the object library. You can only create a derived class of tserverclientthread manually, and then reload the clientexcute method. Procedure tserverthread. clientexcute;
VaR sstream: twinsocketstream;
Sbuffer: array [0 .. 9] of char
Begin
// Obtain and operate commands until the connection is disconnected or the thread is terminated
While (not terminate) and (clientsocket. Active) Do
Begin
Try
Sstream: = twinsocketstream. Create (clientsocket. socket, 60000 );
Try // fill in the sbuffer Array
Fillchar (sbuffer, 10, 0 );
// The delay time is 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: through multi-thread control on the client and server, it is especially convenient for us to process large amounts of information and greatly improve the utilization of network resources.
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.