Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - What is the use of linux tcp to set the receive buffer through setsockopt?
What is the use of linux tcp to set the receive buffer through setsockopt?
An EAGAIN error occurred while executing the socket's send function.

When the client sends a large packet through the send function provided by Socket, it may return an EGGAIN 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.

In order to eliminate this error, there are three options:

1. Make tcp_sendspace greater than the size parameter in send.

- no -p -o tcp_sendspace=65536

2. Before calling send, please set a larger value for SNDBUF in the setsockopt function.

3. use write instead of send, because write will not set O_NDELAY or O_NONBLOCK.

1.tcp transceiver buffer default value

[root @ qljt core]# cat/proc/sys/net/IP v4/TCP _ rmem

4096 87380 4 16 1536

87380: Default value of TCP receive buffer

[root @ qljt core]# cat/proc/sys/net/IP v4/TCP _ wmem

4096 16384 4 16 1536

16384: Default value of TCP send buffer.

2. The maximum buffer of 2.TCP or udp transceiver.

[root @ qljt core]# cat/proc/sys/net/core/rmem _ max

13 107 1

131071:half of the maximum settable value of TCP or udp receive 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,& amprcv _ size & amp; opt len); The value is equal to131071* 2 = 262142.

[root @ qljt core]# cat/proc/sys/net/core/wmem _ max

13 107 1

131071:The TCP or udp send buffer can be set to half the maximum value.

For the same reason.

3. Default value of 3.UDP transceiver buffer

[root @ qljt core]# cat/proc/sys/net/core/rmem _ default

111616: the default value of UDP receive buffer.

[root @ qljt core]# cat/proc/sys/net/core/wmem _ default

1 1 16 16

111616: the default value of UDP send buffer.

Minimum value of tcp or udp transceiver buffer.

The minimum value of tcp or udp receiving buffer is 256 bytes, which is determined by the macro of the kernel;

The minimum value of tcp or udp send buffer is 2048 bytes, which is determined by the macro in the kernel.

Setsockopt sets the socket state.

1.closesocket (usually it doesn't close immediately but goes through the process of TIME_WAIT) wants to continue to reuse the socket:

BOOL bReuseaddr = TRUE

setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(const char *)& amp; bReuseaddr,sizeof(BOOL));

2. If you want to forcibly close the connected socket after calling closesocket, without going through the TIME_WAIT process:

BOOL bDontLinger = FALSE

setsockopt(s,SOL_SOCKET,SO_DONTLINGER,(const char *)& amp; bDontLinger,sizeof(BOOL));

3. In the process of send () send (), recv (), sometimes due to network conditions and other reasons, it is impossible to anticipate sending and receiving, and the time limit for sending and receiving is set:

int nNetTimeout = 1000; // 1 sec

//send time limit

setsockopt(socket,SOL _ S0CKET,SO_SNDTIMEO,(char *)& amp; nNetTimeout,sizeof(int));

//receiving time limit

setsockopt(socket,SOL _ S0CKET,SO_RCVTIMEO,(char *)& amp; 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); Send data in the actual process.

And the amount of received data is relatively large, you can set the socket buffer to avoid the continuous circular sending and receiving of send () send () and recv ();

//Receive buffer

int nRecvBuf = 32 * 1024; //set to 32K

setsockopt(s,SOL_SOCKET,SO_RCVBUF,(const char *)& amp; nRecvBuf,sizeof(int));

//Send buffer

int nSendBuf = 32 * 1024; //set to 32K

setsockopt(s,SOL_SOCKET,SO_SNDBUF,(const char *)& amp; nSendBuf,sizeof(int));

5. If you don't want to experience copying from the system buffer to the socket buffer when sending data, it will affect the performance of the program:

int nZero = 0;

setsockopt(socket,SOL _ S0CKET,SO_SNDBUF,(char *)& amp; nZero,sizeof(nZero));

6. Same as in recv () (by default, the contents of the socket buffer are copied to the system buffer):

int nZero = 0;

setsockopt(socket,SOL _ S0CKET,SO_RCVBUF,(char *)& amp; nZero,sizeof(int));

7. Usually, when sending UDP datagrams, it is expected that the data sent by this socket has broadcast characteristics:

BOOL bBroadcast = TRUE

setsockopt(s,SOL_SOCKET,SO_BROADCAST,(const char *)& amp; 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 connect (), you can set the connect () delay until calling accpet () (this function setting is only meaningful in the non-blocking process.

Function, which has little effect on blocked function calls)

BOOL bConditionalAccept = TRUE

setsockopt(s,SOL_SOCKET,SO_CONDITIONAL_ACCEPT,(const char *)& amp; 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)?

Structural stay {

u _ short l _ onoff

u _ short l _ linger

};

Linger m _ sLinger

m _ slinger . l _ onoff = 1; //(Stop allowed when closesocket () is called, but there is still data to send)

//If m _ slinger.l _ onoff = 0; The function is the same as 2. );

m _ slinger . l _ linger = 5; //(Permitted residence time is 5 seconds)

setsockopt(s,SOL_SOCKET,SO_LINGER,(const char *)& amp; m_sLinger,sizeof(linger));

Set options for windows sockets.

# include & ltwinsock.h & gt

int 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.

Precautions:

The function is used to set the option value of any type and any state of windows socket. 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 operational characteristics by creating a stay structure:

Structural stay {

int l _ onoff

int 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.

Option type meaning

So _ Broadcastebool 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 that setsockopt () does not support are:

Option name type meaning

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:

WSANOTINITIALISED: Before using this API, you should successfully call WSAStartup ().

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.