Document directory
- 1. so_reuseaddr
- 2. so_linger
- 3. so_sndtimeo so_rcvtimeo
- 4. so_rcvbuf so_sndbuf
- 5. so_broadcast
- 6. so_conditional_accept (this option is not available in centos6.3 (linux2.6)
- 7. so_linger
- 8. so_debug
- 9. so_rcvlowat so_sndlowat
- 11. so_keepalive
1. so_reuseaddr
If the socket is already in the established status (usually distinguished by the port number and flag ),
Closesocket (usually does not close immediately and goes through the time_wait process) and wants to continue to reuse the socket:
Bool breuseaddr = true;
Setsockopt (S, sol_socket, so_reuseaddr, (const char *) & breuseaddr, sizeof (bool ));
2. so_linger
If you want to force close a soket that is already in the connection status after you call closesocket
Time_wait process:
/* Bool bdontlinger = false;
Setsockopt (S, sol_socket, so_dontlinger, (const char *) & bdontlinger, sizeof (bool ));*/
Make the following settings in centos6.3 (Linux 2.6:
Struct linger Lin;
Lin. l_onoff = 1; Lin. l_linger = 0;
Setsockopt (sock, sol_socket, so_linger, & lin, sizeof (struct linger ));
3. so_sndtimeo so_rcvtimeo
In the send (), Recv () process, sometimes due to network conditions and other reasons, sending and receiving cannot be expected, but set the sending and receiving time limit:
Struct timeval {
Long TV _sec;/* seconds */
Long TV _usec;/* microseconds */
};
Struct timeval nnettimeout;
Nnettimeout. TV _sec = 2; nnettimeout. TV _usec = 0;
// Sending time limit
Setsockopt (socket, sol_s0cket, so_sndtimeo, & nnettimeout, sizeof (struct timeval ));
// Receiving time limit
Setsockopt (socket, sol_s0cket, so_rcvtimeo, & nnettimeout, sizeof (struct timeval ));
4. so_rcvbuf so_sndbuf
When sending (), the returned bytes are actually sent out bytes (synchronized) or bytes sent to the socket buffer.
(Asynchronous); by default, the system sends and receives data in 8688 bytes (about 8.5 KB) at a time.
When receiving a large amount of data, you can set a socket buffer to avoid the continuous cyclic sending and receiving of send () and Recv:
// Receiving buffer
Int nrecvbuf = 32*1024; // set it to 32 K
Setsockopt (S, sol_socket, so_rcvbuf, (const char *) & nrecvbuf, sizeof (INT ));
// Sending Buffer
Int nsendbuf = 32*1024; // set it to 32 K
Setsockopt (S, sol_socket, so_sndbuf, (const char *) & nsendbuf, sizeof (INT ));
(If you want to avoid the impact of copying data from the system buffer to the socket buffer when sending data
Program performance, you can set the buffer to 0)
Int nzero = 0;
Setsockopt (socket, sol_s0cket, so_sndbuf, (char *) & nzero, sizeof (INT ));
Setsockopt (socket, sol_s0cket, so_rcvbuf, (char *) & nzero, sizeof (INT ));
5. so_broadcast
Generally, when sending a UDP datagram, you want the data sent by the socket to have the broadcast feature:
Bool bbroadcast = true;
Setsockopt (S, sol_socket, so_broadcast, (const char *) & bbroadcast, sizeof (bool ));
6. so_conditional_accept (this option is not available in centos6.3 (linux2.6)
When the client connects to the server, if the socket in non-blocking mode is in the connect () process
To set the connect () latency until accpet () is called (this function is set only when there is a significant non-blocking process)
Function does not play a major role in blocked function calls)
Bool bconditionalaccept = true;
Setsockopt (S, sol_socket, so_conditional_accept, (const char *) & bconditionalaccept, sizeof (bool ));
7. so_linger
If closesocket () is called while sending data (sending () is not completed, and data is not sent ),
The general measure is to "calmly close" Shutdown (S, sd_both), but the data is definitely lost. How to set the program to meet specific requirements?
Application requirements (that is, disable the socket after sending the unsent data )?
Struct linger {
U_short l_onoff;
U_short l_linger;
};
Linger m_slinger;
M_slinger.l_onoff = 1; // (allowed to stay when closesocket () is called, but there is still data not sent)
// If m_slinger.l_onoff = 0, the function is the same as 2;
M_slinger.l_linger = 5; // (the allowable stay time is 5 seconds)
Setsockopt (S, sol_socket, so_linger, (const char *) & m_slinger, sizeof (linger ));
Note: 1. When the latency is set for a non-blocking socket, it is not very useful. It is best not to use it;
2. If you want the program to not experience so_linger, you need to set so_dontlinger, or set l_onoff = 0;
8. so_debug
A program that is rarely used in SDI or dialog can record the debugging information of socket:
(I tested this function not long ago, and the mode information can be saved, including the parameters during socket creation.
Specific protocols and error code can be recorded)
Bool bdebug = true;
Setsockopt (S, sol_socket, so_debug, (const char *) & bdebug, sizeof (bool ));
10. tcp_nodelay
Generally, data is sent only when the system buffer is full.
The buffer zone sends data immediately:
Bool bnodelay = true;
Setsockopt (S, ipproto_tcp, tcp_nodelay, (const char *) & bnodelayt, sizeof (bool ));
9. so_rcvlowat so_sndlowat
// Set to accept low tide
Int recv_min_size = 6;
Int flag = setsockopt (connfd, sol_socket, so_rcvlowat, (void *) & recv_min_size, sizeof (INT ));
// Obtain the settings
Int recv_min_size1;
Socklen_t recv_min_len = sizeof (recv_min_size1 );
Getsockopt (connfd, sol_socket, so_rcvlowat, (void *) & recv_min_size1, & recv_min_len );
11. so_keepalive
Keepalive = 1;
Setsockopt (listenfd, sol_socket, so_keepalive, (void *) & keepalive, sizeof (keepalive ))
There are only some parameters above. For more parameter descriptions, refer to chapter 7 of UNIX Network Programming V1.