1) What should I do if I encounter socket blocking in the thread?
For example, if the socket server accept is blocked, Why cannot the thread exit smoothly?
Solution: Use the keepalive mechanism of socket;
2) How can I check whether the socket connection is disconnected or abnormal?
1) through the heartbeat mechanism;
2) keepalive mechanism;
3) Common socket attributes
So_reuseaddr:
1. When socket1 with the same local address and port is in the time_wait state, and socket2 of the program you start occupies the address and port, your program will use this option.
2. so_reuseaddr allows multiple instances (multiple processes) of the same server to be started on the same port ). However, the IP addresses bound to each instance cannot be the same. This situation can be tested on machines with multiple NICs or with IP alias technology.
3. so_reuseaddr allows a single process to bind the same port to multiple sockets, but each socket is bound with a different IP address. This is very similar to 2. for the difference, see unpv1.
4. so_reuseaddr allows repeated binding of identical addresses and ports. But this is only used for UDP multicast and not for TCP.
So_recvbuf/so_sndbuf
Set the receiving and sending buffer of the socket;
So_rcvtimeo/so_sndtimeo
Setting the sending timeout and receiving timeout of the socket is very useful in blocking the socket to avoid blocking all the time;
So_linger:
Linger, as the name implies, is the delay. Here, it is the delay of the connection-oriented socket close operation. By default, close returns immediately, but when there is still some data in the sending buffer, the system will try to send the data to the peer. So_linger can change the close action. Prevent the socket from entering the time_wait status;
So_keepalive
So_keepalive/tcp_keepcnt/tcp_keepidle/tcp_keepintvl
If one party has closed or terminated the connection abnormally, but the other party does not know, we call this TCP connection semi-open. TCP uses keepalive to detect semi-open connections.
In highly concurrent network servers, the socket is often missed, and a large number of close_wait connections occur. In this case, you can set the keepalive option to solve this problem. Of course, there are other ways to solve this problem.
Tcp_defer_accept
Defer accept, which literally delays accept. In fact, a connection is created only after the first data is received. For non-interactive servers such as HTTP, this makes sense and can be used to defend against null connection attacks (only establish connections, but do not send any data ).
The usage is as follows:
Val = 5;
Setsockopt (srv_socket-> FD, sol_tcp, tcp_defer_accept, & Val, sizeof (VAL ));
The unit of Val is second. Note that if this function is enabled
In Val
If no data is received within seconds, the connection will not be directly discarded instead. If the server sets the tcp_defer_accept option and the server receives a CONNECT request, after three handshakes, the new socket status is still syn_recv instead of established, and the operating system does not accept.
Because after the tcp_defer_accept option is set, the status does not reach established after the three-way handshake, but syn_recv. At this time, if the client has not sent a "data" packet, the server will re-transmit the SYN/ACK packet. ipv4.tcp _ synack_retries parameter control. After the number of retransmissions is reached, the timeout value set in setsockopt will be performed again. Therefore, the syn_recv survival time is greater than the set value.
Tcp_nodelay/tcp_chork:
Cp_nodelay and tcp_cork basically control the "Nagle" of the package. The meaning of Nagle here is that the Nagle algorithm is used to assemble a smaller package into a larger frame. Both tcp_nodelay and tcp_cork disable the Nagle algorithm, but their behavior is different.
Tcp_nodelay does not use the Nagle algorithm. It does not splice a small packet into a large package before sending it. directly sending the small packet will make the user experience very good.
When transmitting a large amount of data, you can set tcp_cork to improve the efficiency of TCP transmission. As the name suggests, cork means "plug-in", which will try to send the maximum data volume each time. When tcp_cork is set, it will block 200 ms. After the blocking time, the data will be automatically transmitted.
4) socket
Connection status
Closed: No connection is active or in progress;
Listen: the server is waiting for a call;
Syn_recv: a connection request has arrived. wait for confirmation;
Syn_sent: The application has started. Open a connection;
Established: normal data transmission status;
Fin_wait1: The application says it has been completed;
Fin_wait2: the other side has agreed to release;
Itmed_wait: wait until all groups die;
Closing: both sides attempt to close at the same time;
Time_wait: the other side has initialized a release;
Last_ack: wait until all groups die;