In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Socket option function
Function: method used to read and set socket file descriptor properties
# include int getsockopt (int sockfd, int level, int option_name, void* option_value, socklen_t* restrict option_len); int setsockopt (int sockfd, int level, int option_name, const void* option_value, socklen_t option_len)
The socket options table is as follows:
The getsockopt and setsockopt functions return 0 on success and-1 on failure and set errno.
For the server, some socket options are valid only for listening socket settings before calling the listen system call. This is because the connection socket can only be returned by the accept call, and the connection accepted by accept from the listen snooping queue has completed at least the first two steps of the TCP three-way handshake (because the connection in the listen snooping queue has at least entered the SYN_RCVD state), indicating that the server has sent an TCP synchronization message segment to the received connection. However, some socket options should be set in the TCP synchronous message segment, such as the TCP maximum message segment option. In this case, linux provides developers with a solution: set these socket options for listening socket, then the connection socket returned by accept will automatically inherit these options. These options include: SO_DEBUG, SO_DONTROUTE, SO_KEEPALIVE, SO_LINGER, SO_OOBINLINE, SO_RCVBUF, SO_RCVLOWAT, SO_SNDBUF, SO_SNDLOWAT, TCP_MAXSEG, and TCP_NODELAY.
For the client, these socket options should be set before calling the connect function, because the TCP three-way handshake is complete after the connect call returns successfully.
SO_REUSEADDR option
I discussed the TIME_WAIT state of the TCP connection earlier and mentioned that the server program can force the use of socket addresses occupied by connections in the TIME_WAIT state by setting the socket option SO_REUSEADDR.
# include # include int main (int argc, char* argv []) {if (argc = 0); int reuse = 1; setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, & reuse, sizeof (reuse)); struct sockaddr_in address; bzero (& address, sizeof (address)); address.sin_family = AF_INET; inet_pton (AF_INET, ip, & address.sin_addr) Address.sin_port = htons (port); int ret = bind (sock, (struct sockaddr*) & address, sizeof (address)); assert (ret! =-1); ret = listen (sock, 5); assert (ret! =-1); struct sockaddr_in client; socklen_t client_addrlength = sizeof (client); int connfd = accept (sock, (struct sockaddr*) & client, & client_addrlength); if (connfd)
< 0 ) { printf( "errno is: %d\n", errno ); } else { char remote[INET_ADDRSTRLEN ]; printf( "connected with ip: %s and port: %d\n", inet_ntop( AF_INET, &client.sin_addr, remote, INET_ADDRSTRLEN ), ntohs( client.sin_port ) ); close( connfd ); } close( sock ); return 0;} 经过setsocketopt的设置之后,即使sock处于TIME_WAIT状态,与之绑定的socket地址也可以立即被重用。此外,我们也可以通过修改内核参数/proc/sys/net/ipv4/tcp_tw_recycle 来快速回收被关闭的socket,从而使得TCP连接根本就不进入TIME_WAIT状态,进而允许应用程序立即重用本地的socket地址。 SO_RCVBUF和SO_SNDBUF选项 SO_RCVBUF和SO_SNDBUF选项分别表示TCP接收缓冲区和发送缓冲区的大小。不过,当我们用setsockopt来设置TCP的接收缓冲区和发送缓冲区的大小时,系统都会将其值加倍,并且不得小于其个最小值。TCP接收缓冲区的最小值是256字节,而发送缓冲区的最小值是2048字节(不过,不同的系统可能有不同的默认最小值)。此外,我们可以直接修改内核参数/proc/sys/net/ipv4/tcp_rmem和/proc/sys/net/ipv4/tcp_wmem来强制TCP接收缓冲区和发送缓冲区的大小没有最小值限制。 修改TCP发送缓冲区的客户端程序: #include #include #include #include #include #include #include #define BUFFER_SIZE 512 int main( int argc, char* argv[] ){ if( argc = 0 ); int sendbuf = atoi( argv[3] ); int len = sizeof( sendbuf ); setsockopt( sock, SOL_SOCKET, SO_SNDBUF, &sendbuf, sizeof( sendbuf ) ); getsockopt( sock, SOL_SOCKET, SO_SNDBUF, &sendbuf, ( socklen_t* )&len ); printf( "the tcp send buffer size after setting is %d\n", sendbuf ); if ( connect( sock, ( struct sockaddr* )&server_address, sizeof( server_address ) ) != -1 ) { char buffer[ BUFFER_SIZE ]; memset( buffer, 'a', BUFFER_SIZE ); send( sock, buffer, BUFFER_SIZE, 0 ); } close( sock ); return 0;} 修改TCP接收缓冲区的服务器程序: #include #include #include #include #include #include #include #include #include #define BUFFER_SIZE 1024 int main( int argc, char* argv[] ){ if( argc = 0 ); int recvbuf = atoi( argv[3] ); int len = sizeof( recvbuf ); setsockopt( sock, SOL_SOCKET, SO_RCVBUF, &recvbuf, sizeof( recvbuf ) ); getsockopt( sock, SOL_SOCKET, SO_RCVBUF, &recvbuf, ( socklen_t* )&len ); printf( "the receive buffer size after settting is %d\n", recvbuf ); int ret = bind( sock, ( struct sockaddr* )&address, sizeof( address ) ); assert( ret != -1 ); ret = listen( sock, 5 ); assert( ret != -1 ); struct sockaddr_in client; socklen_t client_addrlength = sizeof( client ); int connfd = accept( sock, ( struct sockaddr* )&client, &client_addrlength ); if ( connfd < 0 ) { printf( "errno is: %d\n", errno ); } else { char buffer[ BUFFER_SIZE ]; memset( buffer, '\0', BUFFER_SIZE ); while( recv( connfd, buffer, BUFFER_SIZE-1, 0 ) >0) {} close (connfd);} close (sock); return 0;}
Running result:
Root@iZbp1anc6yju2dks3nw5j0Z:~/test/socket#. / client 127.0.0.1 12345 2000
The tcp send buffer size after setting is 4608
Root@iZbp1anc6yju2dks3nw5j0Z:~/test/socket#. / server 127.0.0.1 12345 50
The receive buffer size after settting is 2304
As explained above: when we use setsockopt to set the receive buffer and send buffer of TCP, the system will double its value and not less than its minimum value.
SO_RCVLOWAT and SO_SNDLOWAT option
The SO_RCVLOWAT and SO_SNDLOWAT options represent the low water mark of the TCP receive buffer and send buffer, respectively. They are generally called by the Imax O multiplexing system to determine whether the socket is readable or writable. When the total number of readable data in the TCP receiving buffer is greater than its low water mark, the Imax O multiplexing system call will inform the application that the data can be read from the corresponding socket; when the free space in the TCP sending buffer (the space that can be written to the data) is greater than its low water mark, the IUnip O multiplexing system call will inform the application that the data can be written to the corresponding socket. By default, the low water mark of the TCP receive buffer and the low water mark of the TCP send buffer are both 1 byte.
SO_LINGER option
The SO_LINGER option controls the behavior of close system calls when closing TCP connections. By default, when we use close system call to turn off a socket, the close returns immediately, and the TCP module is responsible for sending the remaining data in the TCP send buffer corresponding to the socket to the other party.
When setting the value of the SO_LINGER option, we need to pass a structure of type linger to the setsockopt (getsockopt) system call, which is defined as follows:
# include struct linger {int lounge off; / / on (non-0) or off (0) this option int lounger; / / residence time}. Depending on the different values of the two member variables in the linger structure, the close system call may produce one of the following three behaviors: l_onoff equals 0. The SO_LINGER option does not work at this time, and close turns off socket with the default behavior. L_onoff is not zero and lumbago equals 0. At this time, the close system call returns immediately, and the TCP module will discard the residual data in the TCP send buffer corresponding to the closed socket and give the other party a reset message segment. Therefore, this situation provides the server with a way to terminate a connection abnormally. The l_onoff is not 0 and the number of lenders is greater than 0. In this case, the behavior of the close depends on two conditions: (1) whether there is any residual data in the TCP send buffer corresponding to the closed socket, and (2) whether the socket is blocking or non-blocking. The blocked socket,close will wait for a long time as l_linger until the TCP module has sent all the residual data and received confirmation from the other party. If the TCP module does not send the residual data in between and is confirmed by the other party, then the close system call will return-1 and set errno to EWOULDBLOCK. If the socket is non-blocking, the close will return immediately, and we need to determine whether the residual data has been sent based on its return value and errno.
The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.