Sigpipe:
Write socketProgramIf you try to send to a disconnected socket, the underlying layer will throw a sigpipe signal.
The default processing method for this signal is to exit the process, which is not what we expect most of the time. Therefore, we need to reload the processing method of this signal. Call the followingCodeTo safely shield sigpipe:
Static int
Disablesigpipe (void)
{
Struct sigaction act, oact;
Act. sa_handler = sig_ign;
Sigemptyset (& act. sa_mask );
Act. sa_flags = 0;
Act. sa_flags | = sa_restart;
If (sigaction (sigpipe, & act, & oact) <0 ){
Error_print_syscall ("Disable: sigaction ");
Return failed;
}
Return successful;
}
Linger:
When the connection is interrupted, You need to delay the linger to ensure that all data is transmitted. Therefore, you need to enable the so_linger option.
The linger structure is defined in/usr/include/Linux/socket. h:
Struct Linger
{
IntL_onoff;/* Linger active */
IntRochelle linger; /* How long to linger */
};
* If l_onoff is 0, the feature will be canceled when the delay is disabled. If the value is not zero, the interface delay is allowed to be disabled.
* The l_linger field indicates the time when the delay is disabled.
Int
Disablelinger (void ){
Struct linger Ling = {0, 0 };
If (setsockopt (sockfd, sol_socket, so_linger, & Ling, sizeof (Ling) <0 ){
Error_print_syscall ("tcpsocket: disablelinger: setsockopt ");
Return failed;
}
Return successful;
}
Nagle Algorithm
According to the name of the creator John Nagle. This algorithm is used to automatically connect a certain number of messages in the buffer zone. This process (called nagling) improves the efficiency of network application systems by reducing the number of packets that must be sent. Nagle algorithm, defined by Ford aerospace and Communications Corporation congestion control in IP/tcp internetworks (ietf rfc 896) (1984), was initially used to buffer Ford's private TCP/IP network congestion, but it is widely spread.
The Nagle document defines a solution to the small packet problem. When an application generates only one byte of data each time, the network is overloaded due to such a small packet (this situation is often referred to as "sending Sb window complications "), this problem occurs. A single character from the keyboard-1 byte of data-may cause a 41-byte packet to be transmitted, which contains 1 byte of useful data and 40 bytes of header data. This 4000% overload is acceptable in a network with a very low load such as apranet, but it may be forced re-transmitted in a network with a heavy load such as Ford, packet loss is caused, and transmission speed is reduced through over-crowded switching nodes and gateways. Furthermore, when the connection is dropped, the throughput may be reduced. Nagle algorithm-the common implementation method is to insert two lines of code in a TCP Program-in the sender, buffer (store) the data identified as unresponsive (this is a strange statement, in fact, it should buffer unsent data in order and splice the data during sending ). The data sent in sequence will be kept to the response that receives the identified data or the whole package of valuable data needs to be sent.
Although the Nagle algorithm is used to solve problems in the Ford network, the same problems also occur in apranet. Through the network, nagling is widely implemented, including the Internet, and has a huge effect-although in some cases, in high interactive environments such as some C/S, it is not expected to be processed. In this case, you can use the tcp_nodelay socket option to disable nagling.
Note: Although Nagle solves the small packet problem, it also causes a high unpredictable delay and reduces the throughput.
Int Tcpsocket: disablenagle (void) { Int val = 1; If (setsockopt (sockfd, ipproto_tcp, tcp_nodelay, (const void *) & Val, sizeof (VAL) <0 ){ Error_print_syscall ("tcpsocket: disablenagle: setsockopt "); Return failed; } Return successful; } |
Http://blog.chinaunix.net/u2/80823/showart_1280185.html