In Windows, the "non-blocking" mode is generally used. For the client socket
Set enttype to ctnonblocking, indicating that the connection is non-blocking. When the service at the other end
When the client socket attempts to read or write data, the client socket will be notified (onread event or onwrite event
).
For server socket, if the servertype attribute is set to stnonblocking
Blocking connection. When the client socket on the other end tries to read or write, the server socket
You will be notified (onclientread event or onclientwrite event ). In the handles that respond to these events
A socket parameter. this parameter is very useful. It is the socket object of the server or client (tclientwi
Nsocket, tserverwinsocket, or tserverclientwinsocket), the common base class of these socket objects
It is tcustomwinsocket, and tcustomwinsocket provides methods to transmit data over the Internet, for example,
To read data, you can call receivebuf or receivetext. To send data, you can call sendbuf, se
Ndstream, sendtext, or sendstreamthenddrop, where sendstreamthenddrop can
The socket is automatically disconnected after the data is collected. Sendstream and sendstreamthendrop are suitable for sending stream objects,
For example, file, however, you do not need to explicitly Delete the stream object, because when the connection between sockets is disconnected, so
The bucket Automatically releases all stream objects.
Unlike the non-blocking method, there are no asynchronous tasks such as onread or onwrite in the blocking mode.
The socket must actively read or write data. Before the read or write operations are completed, the application is in the waiting state,
This will inevitably affect the performance of the entire application.
For client socket, if the clienttype feature is set to ctblocking, it indicates that the blocking side is used.
. To minimize the side effects of blocking
Put it in a separate thread, so that other threads can continue to be executed in a 32-bit Windows environment.
Of course, if the client program itself has nothing to do in reading and writing functions, you do not have to create a thread.
For server socket, if the servertype feature is set to stthreadblocking
Use blocking to connect. c ++ builder 3 automatically allocates a new thread for each blocking connection,
In this way, even if a customer is performing read/write operations, other customers do not have to wait. Server socket
The tserverclientthread object is used to manipulate every thread. When the thread starts to execute, it checks that the thread is located at the other end.
Whether the customer is trying to write. If so, the onclientread event is triggered.
Graph writing triggers the onclientwrite event for the server to write.
In fact, this method of using thread objects to manage connections can be considered as a "false blocking" method.
It is similar to the non-blocking method. What is troublesome is the client. How does it know the other party (the server side)
Are you ready to read or write? This requires the twinsocketstream object. The waitfordata of this object can be called.
To ensure synchronization between servers and customers. If the twinsocketstream object is used for reading or writing
The twinsocketstream object is deemed to have timed out and will be automatically disconnected.
Avoid application lock. Because the twinsocketstream object has a timeout protection mechanism, you can also set TW
The insocketstream object is used by the server to read or write data. Note that the twinsocketstream object is not
It can be used in non-blocking mode.
Some readers may ask, since blocking may affect application performance, although multithreading technology is used
It can be compensated, but it also makes programming complicated. Why should we use the "blocking" method? This is because
In the plug-in mode, the read or write operations are active. After reading or writing operations are completed, you can safely close the socket connection. While
In the "non-blocking" mode, the read or write operations are random, and the socket must always be in the connection status to capture the exception.
Step event. From network!