Set the socket buffer size in Linux

Source: Internet
Author: User
Tags socket error

The socket buffer size provided by the system is 8 K. You can set it to 64 K, especially when transmitting real-time videos.

When the socket sends data, it first sends the data to the socket buffer, and then receives the function to retrieve the data from the buffer. If the sending end is particularly fast, the buffer will soon be filled up (the default socket is 1024 × 8 = 8192 bytes). At this time, we should set the buffer size according to the situation, which can be achieved through the setsockopt function.

 

# Include <stdio. h>
# Include <stdlib. h>
# Include <unistd. h>
# Include <string. h>
# Include <errno. h>
# Include <sys/types. h>
# Include <sys/socket. h>
# Include <assert. h>
 
Int main (INT argc, char ** argv)
{
Int err =-1;/* return value */
Int S =-1;/* socket descriptor */
Int snd_size = 0;/* size of the sending buffer */
Int rcv_size = 0;/* receive buffer size */
Socklen_t optlen;/* length of the option value */
 
/*
* Create a TCP socket
*/
S = socket (pf_inet, sock_stream, 0 );
If (S =-1 ){
Printf ("establish socket error \ n ");
Return-1;
}

/*
* Read the buffer settings first
* Obtain the size of the original sending Buffer
*/
Optlen = sizeof (snd_size );
Err = getsockopt (S, sol_socket, so_sndbuf, & snd_size, & optlen );
If (ERR <0 ){
Printf ("An error occurred while obtaining the sending buffer size \ n ");
}
/*
* Print the original buffer settings
*/
 
/*
* Obtain the size of the original receiving buffer.
*/
Optlen = sizeof (rcv_size );
Err = getsockopt (S, sol_socket, so_rcvbuf, & rcv_size, & optlen );
If (ERR <0 ){
Printf ("receive buffer size error \ n ");
}

Printf ("original sending buffer size: % d byte \ n", snd_size );
Printf ("the original size of the receiving buffer is % d bytes \ n", rcv_size );
 
/*
* Set the sending buffer size.
*/
Snd_size = 10*1024;/* the size of the sending buffer is 8 K */
Optlen = sizeof (snd_size );
Err = setsockopt (S, sol_socket, so_sndbuf, & snd_size, optlen );
If (ERR <0 ){
Printf ("set sending buffer size error \ n ");
}
 
/*
* Set the size of the receiving buffer.
*/
Rcv_size = 10*1024;/* the size of the receiving buffer is 8 K */
Optlen = sizeof (rcv_size );
Err = setsockopt (S, sol_socket, so_rcvbuf, (char *) & rcv_size, optlen );
If (ERR <0 ){
Printf ("Incorrect receiving buffer size \ n ");
}
 
/*
* Check the preceding buffer settings.
* Obtain the size of the sent buffer after modification.
*/
Optlen = sizeof (snd_size );
Err = getsockopt (S, sol_socket, so_sndbuf, & snd_size, & optlen );
If (ERR <0 ){
Printf ("An error occurred while obtaining the sending buffer size \ n ");
}
 
/*
* Received buffer size after modification
*/
Optlen = sizeof (rcv_size );
Err = getsockopt (S, sol_socket, so_rcvbuf, (char *) & rcv_size, & optlen );
If (ERR <0 ){
Printf ("receive buffer size error \ n ");
}
 
/*
* Print the result
*/
Printf ("sending buffer size: % d byte \ n", snd_size );
Printf ("received buffer size: % d byte \ n", rcv_size );
 
Close (s );
Return 0;

}

Result After running:

The original size of the sending buffer is 16384 bytes.

The original size of the receiving buffer is 87380 bytes.
The sending buffer size is 20480 bytes.
The size of the receiving buffer is 20480 bytes.
The result shows that the default sending buffer size of Ubuntu is 16384 bytes, and the receiving buffer is 87380 bytes.

However, the size of the buffer set for receiving and sending is 10*1024 = 10240 bytes, but the actual size of the buffer obtained by getoptsock is doubled by 20480 bytes. The other values are doubled.

This is determined by the Linux Kernel Algorithm.

When the socket sends data, it first sends the data to the socket buffer, and then receives the function to retrieve the data from the buffer. If the sending end is particularly fast, the buffer will soon be filled up (the default socket is 1024 × 8 = 8192 bytes). At this time, we should set the buffer size according to the situation, which can be achieved through the setsockopt function.

 

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.