Linux getsockopt and setsockopt set buffer size __linux

Source: Internet
Author: User
Tags socket error
The system provides a socket buffer size of 8K, which you can set to 64K, especially when streaming live video.

When the socket sends data to send data to the socket buffer, then accept the function and then fetch the data from the buffer, if the sending end is very fast, the buffer will be filled up quickly (the socket default is 1024x8=8192 byte), At this point we should set the size of the buffer according to the situation, we can implement the #include by the setsockopt function <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; /* Send buffer size * *
int rcv_size = 0; /* Receive Buffer size * *
Socklen_t Optlen; /* Option Value length * *

/*
* Create a TCP socket
*/
s = socket (pf_inet,sock_stream,0);
if (s = = 1) {
printf ("Create socket error \ n");
return-1;
}

/*
* Read the buffer settings first
* Get the original send buffer size
*/
Optlen = sizeof (snd_size);
Err = getsockopt (S, Sol_socket, So_sndbuf,&snd_size, &optlen);
if (err<0) {
printf ("Get send buffer size error \ n");
}
/*
* Print original buffer settings
*/

/*
* Get the original receive buffer size
*/
Optlen = sizeof (rcv_size);
Err = getsockopt (S, Sol_socket, So_rcvbuf, &rcv_size, &optlen);
if (err<0) {
printf ("Get receive buffer size error \ n");
}

printf ("The original size of the send buffer is:%d bytes \ n", snd_size);
printf ("The original size of the receive buffer is:%d bytes \ n", rcv_size);

/*
* Set Send buffer size
*/
Snd_size = 10*1024; /* Send buffer size of 8K * *
Optlen = sizeof (snd_size);
Err = setsockopt (S, Sol_socket, So_sndbuf, &snd_size, Optlen);
if (err<0) {
printf ("Set send buffer size error \ n");
}

/*
* Set the Receive buffer size
*/
Rcv_size = 10*1024; /* Receive buffer size 8K * *
Optlen = sizeof (rcv_size);
Err = setsockopt (s,sol_socket,so_rcvbuf, (char *) &rcv_size, Optlen);
if (err<0) {
printf ("Set receive buffer size error \ n");
}

/*
* Check the above buffer settings
* Get modified Send buffer size
*/
Optlen = sizeof (snd_size);
Err = getsockopt (S, Sol_socket, So_sndbuf,&snd_size, &optlen);
if (err<0) {
printf ("Get send buffer size error \ n");
}

/*
* Get the modified receive buffer size
*/
Optlen = sizeof (rcv_size);
Err = getsockopt (S, Sol_socket, SO_RCVBUF, (char *) &rcv_size, &optlen);
if (err<0) {
printf ("Get receive buffer size error \ n");
}

/*
* Print Results
*/
printf ("Send buffer size is:%d bytes \ n", snd_size);
printf ("Receive buffer size is:%d bytes \ n", rcv_size);

Close (s);
return 0;

}

Results after run:

Send buffer original size: 16384 bytes Receive buffer original size: 87380 bytes
Send buffer size is: 20480 bytes
Receive buffer size is: 20480 bytes
The default send buffer size for Ubuntu system is shown in the results: 16384 bytes, receive buffer 87380 bytes

But there's a problem. I set the receive and send buffer size of: 10*1024=10240 bytes, but the actual use of Getoptsock is 20480 bytes plus one times. The other size is doubled.

This is determined by the Linux kernel algorithm.

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.