Network Server FAQs and Solutions

Source: Internet
Author: User

Statement: the original content of this article is from

Http://hi.baidu.com/roxws/blog/item/1f57e52eea0ddc5c4fc226ac.html

1. UDP sending buffer and receiving buffer, which mainly prevents network jitter.

1.
The buffer of the default UDP socket is determined by net. Core. rmem_default (there is no special relationship such as 2x). That is, rmem_default reflects the actual default buffer size (not rmem_max)

SK-> sk_recbuf = sysctl_rmem_defaul;

SK-> sk_sndbuf = sysctl_wmem_defaul;

2. The configuration of Net. Core. rmem_default is not restricted by net. Core. rmem_max, and the actual effect is not limited by rmem_max,
Rmem_max is only used to restrict setsockopt

3.
When the program uses setsockopt to set so_rcvbuf, the actual effect value is set to parameter * 2, and the parameter must not exceed rmem_max (rmem_max is used when the parameter is exceeded)

4. WMEM _ * is the same as above

View More kernel source code. We can see that similar situations also apply to the so_rcvbuf option. The following is an excerpt from the code:
427 case so_rcvbuf:
428
432
433 if (Val> sysctl_rmem_max)
434 val = sysctl_rmem_max;
435 set_rcvbuf:
436 SK-> sk_userlocks | = sock_rcvbuf_lock;
437
452 if (Val * 2) <sock_min_rcvbuf)
453 SK-> sk_rcvbuf = sock_min_rcvbuf;
454 else
455 SK-> sk_rcvbuf = Val * 2;
456 break;

2. Set txqueuelen while setting rmem_default. The length of the transmission easing area.

Rmem_default: Applicable to All socket ports. And pass
Setsockopt only sets the current port. In fact, this number indicates the space occupied by all unsent packets on a port. It is not a real description of a buffer zone.

Txqueuelen: a buffer actually exists. It is a buffer actually exists during the downlink process of the package.

Therefore, the txqueuelen setting should increase according to the WMEM size to ensure that WMEM is used to control the sending buffer, so as to ensure that the buffer is full through sendto failure. Txqueuelen> (WMEM/(Package Size + 250) * Number of sockets. If one network adapter only sends one socket, set WMEM to 1 m and set txqueuelen to 1 W. Setting: ifconfig eth1
Txqueuelen 10000 takes effect immediately after being set. You do not need to restart the process.

Iii. TCP server settings

For general socket settings, see

Http://hi.baidu.com/roxws/blog/item/e2958354bf32df58564e00c0.html

The server must pay special attention to the time_wait issue. When formulating the protocol, try to make the client close first. Note: When the process exits, TCP will send an ending sign to the other party.

# Net. ipv4.tcp _ tw_reuse = 1It is useless to reduce the time_wait wait time. The test shows no usage. This should mainly prevent the restart process from prompting that the port has been bound.

# Net. ipv4.tcp _ tw_recycle = 1Fast Recovery to reduce the time_wait wait time.

Set the so_linger option of the socket:
Note that if there is a resend, the client will increase the number of resends. Therefore, we recommend that you use it with caution !!

This option specifies how the function close operates on connection-oriented protocols (such as TCP ). The default close operation of the kernel returns immediately. If any data remains in the interface buffer, the system will try to send the data to the other party.

The so_linger option is used to change this default setting. Use the following structure:

Struct linger {

Int l_onoff;/* 0 = OFF, nozero = on */

Int l_linger;/* Linger time */

};

There are three situations:

1. If l_onoff is set to 0, this option is disabled. The value of l_linger is ignored, which is equal to the default kernel. The close call will be immediately returned to the caller, if possible, any unsent data will be transmitted;

2. If Rochelle off is set to non-0 and Rochelle linger is set to 0, TCP will terminate the connection when the set interface is closed. TCP will discard any data retained in the buffer zone sent by the set interface and send an RST to the other party, instead of the usual four-group termination sequence, this avoids the time_wait status;

3. Set Rochelle to non-zero and Rochelle to non-zero. When the set of interfaces is disabled, the kernel will be delayed for a period of time (determined by Rochelle linger ). If data remains in the interface buffer, the process will be sleep until (a) all data is sent and confirmed by the other party, then, the normal termination sequence (the descriptive word access count is 0) or (B) delay time is reached. In this case, it is very important for the application to check the return value of close. If the time is up before the data is sent and confirmed, close will return the ewouldblock error, and any data in the sending buffer zone of the interface is lost. The successful return of close only tells us that the data (and fin) sent has been confirmed by the other party's TCP. It does not tell us whether the other party's application process has read the data. If the set of interfaces is set to non-blocking, it will not wait for close to complete.

Struct linger so_linger;
...
So_linger.l_onoff = true;
So_linger.l_linger = 0;
Z = setsockopt (S,
Sol_socket,
So_linger,
& So_linger,
Sizeof so_linger );

Rox thinks it is best to set the TCP server: the client must be used to send the close, and the server does not actively send the close !!! The server only processes cloes that have timed out close and normally received 0 bytes.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.