title: SO_SNDBUF and SO_RCVBUF on Linux sockets
body: <p>I am programming a client server application. Simply, the server can use either UDP or TCP for each client.</p>
  
  <p>I want both the TCP client and UDP client to print the default value for 
  <code>SO_SNDBUF</code> and <code>SO_RCVBUF</code>, but I am surprised when I print the default value for sndbuf and rcvbuf before the connection establishment the value is 0. </p>
  
  <p><strong>Note</strong>: I am using Ubuntu 12.10</p>
  
  <p>Here is my code:</p>
  
  <pre><code>//assume all variables initialized correctly.
  
  /* SO_RCVBUF options */
  
  len = sizeof(rcvbuf);
  getsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &amp;rcvbuf, &amp;len);
  len = sizeof(mss);
  getsockopt(sockfd, IPPROTO_TCP, TCP_MAXSEG, &amp;mss, &amp;len);
  printf("defaults: SO_RCVBUF = %d, MSS = %d\n", rcvbuf, mss);
  
  rcvbuf = 9973;      /* a prime number */
  setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &amp;rcvbuf, sizeof(rcvbuf));
  len = sizeof(rcvbuf);
  getsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &amp;rcvbuf, &amp;len);
  printf("SO_RCVBUF = %d (after setting it to 9973)\n\n\n\n", rcvbuf); 
  
  
  /* SO_RCVBUF options */
  
  
  /* SO_SNDBUF options */
  
  len2 = sizeof(sndbuf);
  getsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &amp;sndbuf, &amp;len2);
  //len2 = sizeof(mss2);
  getsockopt(sockfd, IPPROTO_TCP, TCP_MAXSEG, &amp;mss2, &amp;len2);
  printf("defaults: SO_SNDBUF = %d, MSS = %d\n", sndbuf, mss2);
  
  sndbuf = 9979;      /* a prime number */
  setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &amp;sndbuf, sizeof(sndbuf));
  len2 = sizeof(sndbuf);
  getsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &amp;sndbuf, &amp;len2);
  printf("SO_SNDBUF = %d (after setting it to 9979)\n", sndbuf);
  
  /* SO_SNDBUF options */
  </code></pre>
  
  <p><strong>Sample output:</strong></p>
  
  <blockquote>
    <p>defaults: SO_RCVBUF = 0, MSS = 0</p>
    
    <p>SO_RCVBUF = 9973 (after setting it to 9973)</p>
    
    <p>defaults: SO_SNDBUF = 0, MSS = 0</p>
    
    <p>SO_SNDBUF = 9979 (after setting it to 9979)</p>
    
    <p>tcp_time nes411_server </p>
    
    <p>Current time is (TCP): Mon Apr 22 23:40:46 2013</p>
  </blockquote>
  
  <p>After connection:</p>
  
  <blockquote>
    <p>SO_SNDBUF = 170840, MSS = 16384</p>
  </blockquote>