title: Socket Send And Receive Buffer
body: <p>I tried to learn about the conception of Socket-Send(Receive)-Buffer.And I wrote these codes:
  Client:</p>
  
  <pre><code>int client = socket(AF_INET, SOCK_STREAM, 0);
  int s = getsockopt(client, SOL_SOCKET, SO_SNDBUF, &amp;sendBuffSize, &amp;len);
  int status = connect(client, (struct sockaddr*)&amp;addr, sizeof(struct sockaddr_in));
  printf("The send buff size is : %d.\n", sendBuffSize);
  char buf[100000];
  int n, wn;
  int fd = open("./1.txt", O_RDONLY);
  while ((n = read(fd, buf, sizeof(buf))) &gt; 0) {
      wn = write(client, buf, n);
      printf("Write %d bytes.\n", wn);
  }
  </code></pre>
  
  <p>Server: I set the connected client as <strong>Non-block</strong>,and add this client into the epoll.Once the client sends data to the server, I put the main thread into sleep[ten seconds].</p>
  
  <pre><code>char buf[8192];
  sleep(10);
  int rn;
  while ((rn = read(events[i].data.fd, buf, sizeof(buf))) &gt; 0) {
      printf("Read %d bytes.\n", rn);
  }
  </code></pre>
  
  <p>The client send Buffer size is 16384 and the server receive Buffer size is 20000[setsockopt].</p>
  
  <p>According to the book:The client calls the write function will block if the socket send buffer is full. </p>
  
  <p>But I get the result[Client] :
  <a href="https://i.stack.imgur.com/SxTgU.png" rel="nofollow noreferrer">Result</a></p>
  
  <p>And the server :
  <a href="https://i.stack.imgur.com/E6cec.png" rel="nofollow noreferrer">Result</a></p>
  
  <p>Questions:</p>
  
  <ol>
  <li>Receive buffer size + Send buffer size &lt; 100000; but why the write function do not block?</li>
  <li>Why the server read 8192 + 6808 = 15000 bytes instead of read continuously 8192 bytes?</li>
  </ol>