How to set tcp under linux
E-gain error occurred while executing Socket's send function. When a client sends a large data packet through the send function of Socket, it may return an EAGAIN error. This error occurs because the size of the size variable in the send function exceeds the value of tcp_sendspace. Tcp_sendspace defines the amount of data that an application can cache in the kernel before calling send. When the application sets the O _ NDAY or O _ NOBLOCK attribute in the socket, if the send cache is full, send will return an e-gain error. To eliminate this error, there are three options: 1. Add tcp_sendspace to make it larger than the size parameter in send-no-p-no-p-otcp _ sendspace = 655362. Before calling send, set a large value for SNDBUF in setsockopt function 3. Replace send with write. Because write will not set O_NDELAY or o _ nonblock 1. Default value of TCP transceiver buffer [root @ qljtcore]# cat/proc/sys/ net/IPv4/TCP _ rmem409687380 416153687380: default value of TCP receive buffer [root @ qljtcore] # cat/proc/sys/ Net/IPv4/TCP _ wmem409163844161665438 TCP or udp sending and receiving buffer maximum [root @ qljtcore] # cat/proc/sys/net/core/rmem _ max/kloc-. Kloc-0/131071:half of the maximum settable value of TCP or udp receiving buffer. That is to say setsockopt (s, sol _ socket, so _ rcvbuf,&; rcv _ size & amp; opt len); When rcv_size exceeds 13 107 1, getsockopt (s, sol _ socket, so _ rcvbuf,&; rcv _ size & amp; opt len); The value is equal to131071* 2 = 262142 [root @ qljtcore] # cat/proc/sys/net/core/wmem _ max13/kloc- For the same reason. 3. Default value of UDP transceiver buffer [root @ qljt core]# cat/proc/sys/net/core/rmem _ default116: Default value of UDP receiver buffer [root @ qljtcore] # cat/proc. Sys/net/core/wmem _ default11611616: the default value of UDP send buffer. The minimum value of tcp or udp send and receive buffer is 256 bytes. The minimum value of tcp or udp send buffer is 2048 bytes. It is determined by the macro of the kernel that setsockopt will continue to reuse the socket after setting the socket state of 1.closesocket (usually it will not be closed immediately but will go through the process of TIME_WAIT): boolbrewuseaddr = true setsockopt (s, SOL_SOCKET, SO_REUSEADDR, (constchar *)&; bReuseaddr,sizeof(BOOL)); 2. If the connected soket is forced to close after calling closesocket, it will not go through the process of TIME_WAIT: boolbtonlinger = false; setsockopt(s,SOL_SOCKET,SO_DONTLINGER,(const char *)& amp; bDontLinger,sizeof(BOOL)); 3. In the process of send (), send () and recv (), sometimes it is impossible to predict the sending and receiving due to network conditions and other reasons. Set the sending and receiving time limit: int nnet timeout =1000; // 1 sec//sending time setsockopt(socket, SOL _ S0CKET, SO_SNDTIMEO, (char *)&; nNetTimeout,sizeof(int)); //receiving time setsockopt(socket, SOL _ S0CKET, SO_RCVTIMEO, (char *)&; nNetTimeout,sizeof(int)); 4. When sending (), the bytes actually sent (synchronous) or sent to the socket buffer (asynchronous) are returned; The default transceiver status of the system is 8688 bytes (about 8.5K); In the actual process, the amount of data sent and received is relatively large, so socket buffer can be set to avoid continuous circular sending and receiving of send (), recv():// receiving buffer int nRecvBuf=32* 1024; //Set to 32ksetsockopt (s, sol _ socket, so _ rcvbuf, (constchar *)&; nRecvBuf,sizeof(int)); //send buffer int nsendbuf = 32 *1024; //Set to 32ksetsockopt (s, sol _ socket, so _ sndbuf, (constchar *)&; nSendBuf,sizeof(int)); 5. If you don't want to experience copying from the system buffer to the socket buffer when sending data, the performance of the program will be affected: int nZero = 0;; setsockopt(socket,SOL _ S0CKET,SO_SNDBUF,(char *)& amp; nZero,sizeof(nZero)); 6. Complete the above functions in recv () as above (the default is to copy the contents of the socket buffer to the system buffer): int nZero = 0;; setsockopt(socket,SOL _ S0CKET,SO_RCVBUF,(char *)& amp; nZero,sizeof(int)); 7. Generally, when sending UDP datagrams, it is expected that the data sent by socket will have broadcast characteristics: boolbast = true setsockopt (s, SOL_SOCKET, SO_BROADCAST, (constchar *)&; bBroadcast,sizeof(BOOL)); 8. In the process of connecting the client to the server, if the non-blocking mode socket is in the process of connection (), you can set the connect () delay until calling accpet () (this function setting only plays a significant role in the non-blocking process, but has little effect in the blocked function call) boolbcontinentalreception = true setsockopt (s, SOL_SOCKET, SO_CONDITIONAL_ACCEPT). bConditionalAccept,sizeof(BOOL)); 9. If closesocket () is called in the process of sending data (send () is not completed, and there is still data not sent), we usually take the measure of "calmly closing" shutdown(s, SD_BOTH) before, but the data is definitely lost. How to set the program to meet the requirements of a specific application (that is, close the socket after sending unfinished data)? struct linger { u _ short l _ onoffu _ short l _ linger}; Linger m _ slinger _ slinger.l _ onoff =1; //(called in closesocket (), but allowed to stay when there is still data to send)//if m _ slinger.l _ onoff = 0; The function is the same as 2. ); m _ slinger . l _ linger = 5; //(allowed residence time is 5 seconds) setsockopt (s, sol _ socket, so _ linger, (constchar *)&; m_sLinger,sizeof(linger)); Set options for windows sockets. # include & ltwinsock.h & gtint PASCAL FAR setsockopt( SOCKET s,int level,int optname,const char FAR* optval,int opt len); S: a descriptive word that identifies a windows socket. Level: the level defined by the option; Currently only SOL_SOCKET and IPPROTO_TCP layers are supported. Optname: the option to set. Optval: Pointer to the buffer where option values are stored. Optlen: the length of the optval buffer. Note: The setsockopt () function is used to set the option value of any type and any state of windows sockets. Although there are options at different protocol layers, this function only defines the options at the highest "windows Sockets" level. Options affect the operation of windows Sockets, such as whether to receive emergency data in normal data stream, whether to send broadcast data from windows Sockets, and so on. Windows sockets has two options: one is Boolean option, which allows or disables a feature; The other is plastic or structural options. If the Boolean option is allowed, optval points to a non-zero integer; Option optval is prohibited from pointing to an integer equal to zero. For Boolean options, optlen should be equal to sizeof (int); For other options, optval refers to an integer or structure containing the desired option, while optlen refers to the length of the integer or structure. The SO_LINGER option is used to control the operation when there is queued data to send on the windows socket and the closesocket () call has been executed. See the influence of SO_LINGER option in closesocket () function on the semantics of closesocket (). The application sets the corresponding operation characteristics by creating a linger structure: struct Linger {int l _ onoffint l _ Linger}; To allow SO_LINGER, the application should set l_onoff to a non-zero value, set l_linger to zero or the required timeout value (in seconds), and then call setsockopt (). In order to allow SO_DONTLINGER (that is, prohibit SO_LINGER), you should set l_onoff to zero, and then call setsockopt (). By default, windows sockets cannot be bound to local addresses that are already in use (see bind ()). But sometimes you need to reuse this address. Because each connection is uniquely determined by the combination of local address and remote address, it doesn't matter if two windows sockets are bound to one address as long as the remote addresses are different. In order to tell the WINDOWS socket implementation not to bind an address to another WINDOWS socket just because it has already been used by another Windows socket, the application can set the SO_REUSEADDR option before calling bind (). Note that this option is only interpreted when bind () is called; Therefore, if bind () has no effect on this or other windows sockets, it is not necessary (but harmless) to set this option for windows sockets that do not use this address, or to set or clear this option. By turning on the SO_KEEPALIVE option, the application can enable the Windows socket implementation to allow the use of the "keep-alive" package under TCP connections. The implementation of WINDOWS windows sockets does not necessarily support "keep alive", but if it does, the specific semantics will be related to the implementation and should conform to the specifications in the 4.2.3.6 part of RFC 1 122 "Internet Host Requirements-Communication Layer". If the connection fails due to "keep alive", any ongoing call to the windows socket will return a WSAENETRESET error, and any subsequent call will return a WSAENOTCONN error. The TCP_NODELAY option prohibits Nagle algorithm. Nagle algorithm reduces the number of small packets sent by the host by storing unconfirmed data in the buffer until a packet is stored and sent together. But for some applications, this algorithm will reduce the system performance. So TCP_NODELAY can be used to turn off this algorithm. Application writers only set the TCP_NODELAY option when they know its effect exactly and really need it, because it has obvious negative impact on network performance. TCP_NODELAY is the only option that uses IPPROTO_TCP layer, and all other options use SOL_SOCKET layer. If the SO_DEBUG option is set, WINDOWS windows socket vendors are encouraged (but not required) to provide debugging information for the output. However, the mechanism of generating debugging information and the form of debugging information are beyond the scope of this specification. Setsockopt () supports the following options. Where "type" represents the data type referenced by optval. The option type means that SO_BROADCAST BOOL allows windows sockets to transmit broadcast information. SO_DEBUG BOOL records debugging information. So _DONTLINER BOOL don't block the closing operation just because the data is not sent. Setting this option is equivalent to setting the l_onoff element of SO_LINGER to zero. SO_DONTROUTE BOOL prohibits routing; Direct transmission. SO_KEEPALIVE BOOL sends a "KEEPALIVE" packet. SO_LINGER struct linger FAR* If there is unsent data when closing, stop. SO_OOBINLINE BOOL receives the out-of-band data in the conventional data stream. SO_RCVBUF int determines the size of the received buffer. So _ reuse addbool allows windows sockets to bind to an address that is already in use (see bind ()). SO_SNDBUF int specifies the size of the send buffer. TCP_NODELAY BOOL prohibits sending merged Nagle algorithm. BSD options not supported by setsockopt () are: the option name type indicates that so _ accept connbool Windows Sockets is listening. SO_ERROR int gets the error status and clears it. So _ rcvlowatin receives the low-level watermark. SO_RCVTIMEO int receive timeout. So _ sndlowatin sends a low-level watermark. SO_SNDTIMEO int send timeout. SO_TYPE int windows socket type. IP_OPTIONS sets the options in the IP header. Return value: If no error occurs, setsockopt () returns 0. Otherwise, a SOCKET_ERROR error is returned, and the application can obtain the corresponding error code through WSAGetLastError (). Error code: wsanotinialized:wsa startup () should be successfully called before using this API. WSAENETDOWN:WINDOWS Sockets implementation of WINDOWS detected a network subsystem failure. WSAEFAULT:optval is not a valid part of the process address space. WSAEINPROGRESS: A blocked WINDOWS windows socket call is running. Wsaeinvalid:level value of level is illegal, or the information in optval is illegal. WSAENETRESET: Connection timed out after setting SO_KEEPALIVE. WSAENOPROTOOPT: Unknown or unsupported option. The SO_BROADCAST option is not supported by SOCK_STREAM type windows sockets, and the SO_DONTLINGER, SO_KEEPALIVE, SO_LINGER and SO_OOBINLINE options are not supported by SOCK_DGRAM type windows sockets. WSAENOTCONN: When SO_KEEPALIVE is set, the connection is reset. WSAENOTSOCK: descriptor is not a windows socket.