ExploitationWinsock programming is implemented in synchronous and asynchronous modes with clear logic and focus on applications. In the preemptive multi-task operating system (WinNt and Win2K) the efficiency of using multi-thread mode basically reaches the level of asynchronous mode. The following are the main points of Synchronous Programming.
1. fast communication
The Nagle algorithm of Winsock reduces the transmission speed of small data packets. By default, the system uses the Nagle algorithm and
Int setsockopt (
SOCKET s,
Int level,
Int optname,
Const char FAR * optval,
Int optlen
); Function to close it
Example:
SOCKET sConnect;
SConnect =: socket (AF_INET, SOCK_STREAM, IPPROTO_TCP );
Int bNodelay = 1;
Int err;
Err = setsockopt (
SConnect,
IPPROTO_TCP,
TCP_NODELAY,
(Char *) & bNodelay,
Sizoeof (bNodelay); // No latency algorithm is used.
If (err! = NO_ERROR)
TRACE ("setsockopt failed for some reason ");;
2. SegMentSize of the SOCKET and the sending and receiving buffer
TCPSegMentSize is the maximum length of a single datagram when sending and receiving data. The default value is 1460, and the size of the buffer for sending and receiving data is 8192.
InIn SOCK_STREAM mode, if more than 1460 data is sent at a time, the system divides the data into multiple data packets for transmission. The other party receives a data stream, and the application needs to increase the frame disconnection judgment. Of course, you can change the size of 1460 by modifying the Registry. However, MicrcoSoft considers 1460 as the optimal efficiency parameter and does not recommend modification.
In the industrial control system, it is recommended to disableThe Nagle algorithm sends less than 1460 bytes of data each time (1400 is recommended). Therefore, each time a complete data packet is sent, the other party can process the broken frame of the data stream.
3. Reduce the blocking time of the connect function when the network is disconnected during synchronization.
Network disconnection in synchronous ModeThe connection blocking time is about 20 seconds. You can use gethostbyaddr to determine whether the path of the service host is successful or ping the IP address of the host.
A. the blocking time using gethostbyaddr is about 4 seconds regardless of whether the blocking time is successful or not.
Example:
LONG lPort = 3024;
Struct sockaddr_in ServerHostAddr; // service host address
ServerHostAddr. sin_family = AF_INET;
ServerHostAddr. sin_port =: htons (u_short (lPort ));
ServerHostAddr. sin_addr.s_addr =: inet_addr ("192.168.1.3 ");
HOSTENT * pResult = gethostbyaddr (const char *)&
(ServerHostAddr. sin_addr.s_addr), 4, AF_INET );
If (NULL = pResult)
{
Int nErrorCode = WSAGetLastError ();
TRACE ("gethostbyaddr errorcode = % d", nErrorCode );
}
Else
{