Set of interface options (http:www.fanqiang.com)

xiaoxiao2021-03-06  35

This article comes from the socket option: http: //sunsland.top263.net Author: (2001-10-23 09:00:00)

Get and set the function that affects the interface option: getSockOpt: Get a socket option setsockopt: Get a socket option Fcntl: Set the socket for non-blocking I / O signal driver I / O-like OICTL socket option SO-Keepalive SO- Linger SE-RCVBUF and SO-SNDBUF SO-RCVWAT and SO-SNDLOWAT SO-RCVTIMEO and SO-SNDTIMEO SO-ReuseAddr and So-Reusport IP-TTL TCP-Keepalive

getsockopt and setsockopt get socket option: int getsockopt (int sockfd, int level, int optname, void * optval, socklen_t * opteln) to set socket options: int setsockopt (int sockfd, int level, int optname, const void * optval, Socklen_t * Opteln) SockFD: Points to an open socket description word level: (Level): Specify the type of option code. SOL_SOCKET: Basic Set Ipproto_ip: IPv4 Set Ipproto_IPv6: IPv6 Set Ipproto_tcp: TCP Set Optname: Option Name OptVal: It is a pointer type pointing to a variable type: shaping, socket structure, other structure Type: linger {}, timeval {} optlen (option length): Optval size Return Value: Sign Open or close a feature binary option

Check the program output set of interface options: Define integration socket options Call GetSockOpt Output Sets Interface Option Definition Union: Different socket options have different types of UNION VAL {// socket options may have 5 types As a member: int i_val; long; struct linger linger_val; struct timeVal timeval_val; // struct {int s; int us}} Val; // Function prototype (prototype), these functions are used for Value static char * sock_str_flag (union val *, int); // static function can only be called Static Char * Sock_STR_INT (Union Val *, int); static char * sock_str_linger (union val) *, int); Static Char * SOCK_STR_TIMEVAL (Union Val *, int); // Defines the structure SOCK_OPTS, which contains all information for obtaining or outputting a socket option Struct Sock_opts {char * OPT_STR; / / Character Name Int Opt_level; / / Level INT OPT_NAME; // Name Char * (* OPT_VAL_STR) (Union Val *, INT); // Function Pointer, used to output,} // Define structural arrays and initialize Struct Sock_opts Sock_OPTS [] = {// Global variable array can initialize "SO_BROADCAST", SOL_SOCKET, SO_BROADCAST, sock_str_flag, "SO_DEBUG", SOL_SOCKET, SO_DEBUG, sock_str_flag, #ifdef SO_REUSEPORT // macro definition "SO_REUSEPORT" with compile-time, SOL_SOCKET, SO_REUSEPORT, sock_str_flag, #else // no This option "so_reuseport", 0, 0, null, // null means no define #ENDIF "so_type", SOL_Socket, SO_TYPE, sock_str_int, "IP_TTL", IPPROTO_IP, IP_TTL, sock_str_int, "TCP_MAXSEG", IPPROTO_TCP, TCP_MAXSEG, sock_str_int, NULL, {/ * end flag * /} 0, 0, NULL}; source: int main (int argc, char * * argv) {INT FD, LEN; STRUCT SOCK_OPTS * PTR; / / Structure Type FD = Socket (AF_INET, SOCK_STREAM, 0); // Get Socket for (PTR = Sock_opts; Ptr-> OPT_STR! = NULL; PTR ) {// From the first option to the last Printf ("% s:", ptr-> opt_str); // Output character name if (PTR-> OPT_VAL_STR == Null) // No defined case printf (" (undefined) / n "); else {len = sizeof (val); // Get a socket option if (GetSockopt (fd, ptr-> opt_level, ptr-> opt_name, val, len) == -1) {/ / Return value is 1, function call failed err_ret ("

GetSockOpt Error ");} // Output Options Else Printf (" DEFAULT =% S / N ", (* Ptr-> OPT_VAL_STR) (& Val, LEN));}} // End of for loop exit 0);} static char strros [128]; // static variable, reserved the original value static char * sock_str_flag (union val * ptr, int LEN) // length after the function is called after the function is called. {Ix! = Sizeof (int)) // Length Do not match SnPrintf (Strres, SIZEOF (Strres), "Size (% D) NOT SIZEOF (INT", LEN); ELSE // Conversion String Snprintf (Strres, SizeOf (Strres), "% S", (PTR- > i_val == 0)? "OFF": "on"); return (strres);}

Basic Set Interface Options SO_KEEPALIVE detects whether the other party crashes, avoiding (servers) always blocked the input of the TCP connection. After setting this option, if there is no data exchange in any direction of this interface within 2 hours, the TCP will automatically send the other party to keep the survival probe (Keepalive Probe). This is a TCP section that must be in response to the TCP segmentation. It will lead to the following situations: the other party receives everything: responds at a desired ACK. After 2 hours, TCP will issue another detection segment. The other party has collapsed and has been restarted: responded in RST. The standby error of the socket is set to ECONNRESET, and the socket itself is turned off. The other party has no response: the TCP derived from the Berkeley sends another 8 probing sections, separated by 75 seconds, trying to get a response. After the first probe is 11 minutes for 11 minutes, if there is still no response. The homing error of the socket is set to ETIMEOUT, and the socket itself is turned off. If the ICMP error is "host unreachable", indicating that the other host does not crash, but it is not up to reach, and this situation is allowed to be handled as Ehostunreach. SO_RCVBUF and SO_SNDBUF Each socket has a transmission buffer and a receiving buffer. The receiving buffer is used by TCP and UDP to be read from the received data to read by the application process. TCP: TCP advertises the window size of the other end. The TCP socket receiving buffer cannot overflow because the other party does not allow data that exceeds the sized window size. This is the traffic control of TCP. If the other party is ignored by the window size, the receiving TCP will discard it. UDP: This datagram is discarded when the received data is not in the interface to receive the buffer. UDP is no traffic control; fast senders can easily submerge slow recipients, resulting in the receiver's UDP discarding datagram. SO_LINGER Specifies how to operate the protocol connected to the function close - the processing LINGER structure Struct linger = OFF, nonzero = on int LINGER; // linger time when the data remains in the socket transmission buffer In Seconds}; SO_RCVLOWAT and SO_SNDLOWAT Each socket has a low tide limit and a hypotoma limit. They are used by the function Selectt, and the reception low tide limit is to return Select to "readable" and must have the total amount of data that must be in the reception buffer. - For a TCP or UDP socket, this value defaults to 1. Sending low tide limits is to let SELECT returns "writable" and there must be available space in the socket transmission buffer. For TCP socket, this value is often default 2048. For UDP uses low tide limit, since the number of bytes available in its transmit buffer is never changed, as long as the UDP socket transmits buffer size greater than the low tide limit of the socket, such a UDP socket is always writable. . UDP does not send buffers, only the size of the transmit buffer.

TCP socket option TCP_KEEPALIVE Specifies the connection free time of the TCP starts in seconds before the semaphore segmentation. The default must be at least 7200 seconds, ie 2 hours. This option is only valid when the SO_KEPALIVEE socket option is turned on. TCP_MAXSEG Gets or sets the maximum segment size (MSS) of the TCP connection. The return value is the maximum amount of data sent to the other end, which is often used by the other end with the SYN bars' advertisement, unless our TCP selection uses a small value of the MSS than the other party. If this value acquires before the socket connection, the return value is the default value used without receiving the MSS option from the other--side. The letter smaller than this return value may be truly used on the connection, because if the timestamp option is used, it takes up 12-byte TCP option capacity on each segment. Our TCP will change the maximum amount of data of each of the sections sent to the connection, but the premise is that the TCP is to support the path MTU discovery function. If the path to the other party changes, this value can be adjusted up and down. Example: // Get the transmission buffer size and MSS size, set the transmission buffer size // Get the transmission buffer size and MSS size #include "unp.h" #include

/ * for TCP_MAXSEG * / INT Main (int Argc, char ** argv) {int SockFD, RCVBUF, MSS; Socklen_t Len; struct sockaddr_in servaddr; if (argc! = 2) err_quit ("Usage: RCVBUF

"); Sockfd = Socket (AF_INET, SOCK_STREAM, 0); len = sizeof (rcvbuf); Getsockopt (sockfd, SOL_SOCKET, SO_RCVBUF, rcvbuf, len); len = sizeof (mss); Getsockopt (sockfd, IPPROTO_TCP, TCP_MAXSEG, & mss , & len); Printf ("defaults: so_rcvbuf =% D, MSS =% D / N", RCVBUF, MSS); Bzero (servaddr, sizeof (servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = HTONS (13 ); / * daytime server * / inet_pton (AF_INET, argv [1], servaddr.sin_addr); Connect (sockfd, (SA *) & servaddr, sizeof (servaddr)); len = sizeof (rcvbuf); getsockopt (sockfd, SOL_SOCKET , SO_RCVBUF, & RCVBUF, & LEN); LEN = SizeOf (MSS); GetSockopt (SockFD, Ipproto_TCP, TCP_MAXSEG, & MSS, & LEN); Printf ("After Connect: SO_RCVBUF =% D, MSS =% D / N", RCVBUF, MSS EXIT (0);

// Set the send buffer size #include "unp.h" #include

/ * for TCP_MAXSEG VALUE * / INT Main (int Argc, char ** argv) {Int Sockfd, MSS, SendBuff; Socklen_t Optlen; Float Kk; SockFD = Socket (AF_INET, SOCK_STREAM, 0); / * Fetch and print the TCP . maximum segment size * / optlen = sizeof (mss); sendbuff = 2048; Setsockopt (sockfd, SOL_SOCKET, SO_SNDBUF, sendbuff, sizeof (sendbuff)); optlen = sizeof (sendbuff); getsockopt (sockfd, SOL_SOCKET, SO_SNDBUF, sendbuff, & optlen; Printf ("after send buffer size =% d / n", sendbuff); exit (0);} (http://www.fanqiang.com)

转载请注明原文地址:https://www.9cbs.com/read-54695.html

New Post(0)