SetSockOpt Method
[CPP]View Plain copy print? #include
intSetSockOpt (
intSocket
intLevel
intOption_name,
Const
void*option_value,
size_tOption_len);
The first parameter socket is a socket descriptor.
The second parameter level is the levels of the options that are set, and if you want to set an option at the socket level, you must set the degree to Sol_socket.
The third parameter option_name specifies the options to prepare for the option_name, depending on the level, in the case of the Linux 2.6 kernel (which may be different on different platforms) at the socket level (Sol_socket) :
1. So_broadcast Socket Options
This option opens or disables the ability of the process to send broadcast messages. Only datagram sockets support broadcasting, and must also be on a network that supports broadcast messages (such as Ethernet, Token Ring network, etc.). It is not possible to broadcast on point-to-point links, or on the basis of connected transport protocols such as TCP and SCTP.
2. So_debug Socket Options
This option is supported only by TCP. When this option is turned on for a TCP socket, the kernel retains detailed tracking information for all packets that TCP sends and accepts in the socket. This information is stored in a ring buffer in the kernel and can be checked using the TRPT program.
3. So_keepalive Socket Options
When a TCP socket setting is persisted, TCP automatically sends a live detection section to the End-to-end if there is no data exchange on either side of the socket up to 2 hours. This is a TCP section that must be appropriate for the end-to-end, which can cause one of the following 3 situations.
(1) to the end with the desired ACK response. The application process is not notified ( because everything is OK ). TCP will issue another probe section after another 2 hours after it is still silent.
(2) The end-to-end response to the RST, which tells the TCP: the End-to-end has crashed and restarted. The socket's pending error is set to Econnreset, and the socket itself is closed.
(3) There is no response to the section to maintain survival detection.
If there is no response to the probe section of TCP at all, the pending error for the socket is set to etimeout, and the socket itself is closed . However, if the socket receives an ICMP error as a response to a probing section, it returns the response error, and the socket itself is closed.
The function of this option is to detect whether the End-to-end host crashes or becomes unreachable (such as dial-up modem connection drop, power failure, etc.). If the end-to-end process crashes, its TCP sends a fin across the connection, which can be easily detected by calling select.
This option is typically used by the server, but can also be used by customers . The server uses this option because they spend most of their time blocking input waiting to cross the TCP connection, that is, waiting for the customer's request. However, if the client host is disconnected, the power is off or the system crashes, the server process will never know and will continue to wait for the input that never arrives. We call this a semi open connection. The survival option will detect these half-open connections and terminate them.
4. So_linger Socket Options
This option specifies how the close function operates on connection-oriented protocols such as TCP and SCTP, but not UDP. The default action is close to return immediately, but if there is data remaining in the socket send buffer, the system will try to send the data to the end-to-end.
So_linger If you select this option, close or shutdown will not return until the message queued in all sockets is sent successfully or the delay time arrives. Otherwise, the call is returned immediately.
The parameter of this option (Option_value) is a linger structure:
[CPP] view plain copy print? struct Linger { int l_onoff; int L_linger; };
5. So_rcvbuf and SO_SNDBUF socket options
Each socket has a send buffer and a receive buffer.
The receive buffer is used by TCP,UDP and sctcp to hold the received data until it is read by the application process. For TCP, the size of the socket receive buffer free space limits the window size of the TCP advertisement to the end. The TCP socket receive buffer cannot overflow because it is not allowed to emit data that exceeds the size of the advertised window on the end. This is the TCP traffic control, and if the side ignores the window size and emits more data than the window size, this TCP will discard them . However, for UDP, the datagram is discarded when the received datagram is not loaded into the socket receive buffer. in retrospect, UDP is not flow control: the faster the sender can easily drown the slower receiver, resulting in the receiver's UDP discard datagram.
These two socket options allow us to change the default size of the two buffers. For different implementations, the default size can vary greatly. If the host supports NFS, the size of the UDP send buffer often defaults to a value of around 9000 bytes, and the UDP receive buffer often defaults to a value of around 40000 bytes.
The order of the function calls is important when you set the size of the TCP socket receive buffer. This is because the TCP export sizing option is used when establishing the connection with the SYN section and the End-to-end interchange. for customers, this means that the SO_RCVBUF option must be set before calling connect , and for the server, this means that the option must be set to the listener socket before calling listen . Setting this option to a connected socket has no effect on the possible export sizing options because accept will not create and return a connected socket until TCP's three-way handshake is played. This is why you must set this option for a listening socket.
6. So_rcvlowat and So_sndlowat socket options
Each socket also has a receive low watermark and a send low water mark. They are used by the Select function, and these two socket options allow us to modify these two low watermark marks.
receiving a low watermark is the amount of data required in the socket receive buffer when the select returns "readable" . For TCP,UDP and SCTP sockets, the default value is 1. sending a low watermark is the amount of free space that is required in the socket send buffer when the Select returns writable. for TCP sockets, the default value is usually 2048. UDP also uses the Send low water mark, however, because the number of bytes of free space in the send buffer of the UDP socket does not change (meaning that UDP is not a copy of the datagram passed to it by the application process), as long as the send buffer size of a UDP socket is larger than the low watermark of the socket, The UDP socket is always writable. We remember that UDP does not send a buffer, but only sends the buffer size this property.
7. So_rcvtimeo and So_sndtimeo socket options
These two options allow us to set a time-out value for receiving and sending sockets. Note that the arguments to access their getsockopt and setsockopt functions are pointers to the timeval structure, the same as the parameters used in the Select. This allows us to specify the timeout by the number of seconds and subtleties. We prohibit timeouts by setting their values to 0s and 0μs. By default, both timeouts are prohibited.
The Receive timeout affects 5 input functions: Read,readv,recv,recvfrom and Recvmsg. The Send timeout affects 5 output functions: Write,writev,send,sendto and sendmsg.
8. SO_REUSEADDR and So_reuseport socket options
The SO_REUSEADDR socket option can play the following 4 different functions.
(1) SO_REUSEADDR allows a listening server to be started and bundled with its well-known ports, even if the previously established connection to use the port as their local port still exists. This condition is usually met in this way:
(a) Launching of a listening server;
(b) The connection request arrives, deriving a subprocess to process the client;
(c) The listener server terminates, but the child process continues to serve the customers on the existing connection;
(d) Restart the listening server.
By default, the bind call fails when the listener server restarts at step d by calling Socket,bind and listen, because he is trying to bundle a port on an existing connection (that is, a connection being processed by the child process that was previously derived). However, if the server sets the SO_REUSEADDR socket option between the socket and bind two calls, it will succeed. All TCP servers should specify this socket option to allow the server to be restarted in this case.
(2) SO_REUSEADDR allows multiple instances of the same server to be started on the same port, as long as each instance bundles a different local IP address. For TCP, it is absolutely impossible to start multiple servers bundled with the same IP address and the same port number: This is a completely repetitive bundle, even if we set the SO_REUSEADDR socket to the second server.
(3) SO_REUSEADDR allows a single process to bundle the same port to multiple sockets, as long as each bundle specifies a different local IP address.
(4) SO_REUSEADDR allows full repetition of bundles: When an IP address and port number are bound to a socket, the same IP address and port can be bundled onto another socket if the transport protocol supports it. In general, this feature only supports UDP sockets.