Skip to content

Instantly share code, notes, and snippets.

@SLonger
Last active April 11, 2017 09:26
Show Gist options
  • Save SLonger/b9e7493665e7a24a26080ba23b895f6b to your computer and use it in GitHub Desktop.
Save SLonger/b9e7493665e7a24a26080ba23b895f6b to your computer and use it in GitHub Desktop.
the function the follow utility:
1. inquire socket buffer before set socket buffer
2. set socket buffer
3. get the info after set buffer
conclusion: a. the get value of socket buffer is double of the set value.
b. if set value > the max of os max value , the value we get is double *maxsocketbuffer of os
for example
if max of os socket buffer is 200000 bytes and we set value is 3000000 ,
then we get the value is 400000 not 600000.
#include <sys/socket.h>
#include <stdio.h>
int main(int argc, char **argv)
{
int sockfd, sendbuff;
socklen_t optlen;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sockfd == -1)
printf("Error");
int res = 0;
// Get buffer size
optlen = sizeof(sendbuff);
res = getsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &sendbuff, &optlen);
if(res == -1)
printf("Error getsockopt one");
else
printf("send buffer size = %d\n", sendbuff);
// Set buffer size
sendbuff = 98304;
printf("sets the send buffer to %d\n", sendbuff);
res = setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &sendbuff, sizeof(sendbuff));
if(res == -1)
printf("Error setsockopt");
// Get buffer size
optlen = sizeof(sendbuff);
res = getsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &sendbuff, &optlen);
if(res == -1)
printf("Error getsockopt two");
else
printf("send buffer size = %d\n", sendbuff);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment