Original link: http://blog.csdn.net/maopig/article/details/6982457
The system provides a socket buffer size of 8K, which you can set to 64K, especially when transferring live video.
When the socket sends the data to the socket buffer, then accepts the function and then takes the data from the buffer, if the sender is very fast, the buffer is quickly filled (the socket default is 1024x8=8192 bytes), At this time we should set the buffer size according to the situation, can be implemented 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>intMainintargcChar**argv) { intErr =-1;/*return value*/ ints =-1;/*Socket Descriptor*/ intSnd_size =0;/*Send buffer size*/ intRcv_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 ("set up socket error \ n"); return-1; } /** First read the buffer settings * 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 Raw 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 ("Send buffer original size is:%d bytes \ n", snd_size); printf ("Receive buffer original size is:%d bytes \ n", rcv_size); /** Set Send buffer size*/snd_size=Ten*1024x768;/*Send buffer size is 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=Ten*1024x768;/*Receive buffer size is 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 status of 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"); } /** Receive buffer size after modification*/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;}
The results after the run are as follows:
The result shows Ubuntu system default send buffer Size: 16384 bytes, receive buffer 87380 bytes
But there's a problem. I set the receive and send buffer size to: 10*1024=10240 bytes, but the actual use of Getoptsock obtained is 20480 bytes plus one times. Change to other sizes is also doubled.
This is determined by the algorithm of the Linux kernel.
Linux sets the size of the socket buffer (GO)