title: c socket: send() send only 1 byte at a time
body: <p>I have the following program, that has to read a file 1MB at a time, send it to a server (always 1MB at a time) and get back an hash code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;fcntl.h&gt;
  #include "Socket.h"
  #include "../utils.h"
  
  char *prog_name;
  
  int main (int argc, char *argv[])
  {
      int fd, hash, n=atoi(argv[3]);
      char buffer[1048576];
      Socket socket=SCreate(TCP);
      Connect(socket, argv[1], atoi(argv[2]));
      fd=open(argv[4], O_RDONLY);
      if(read(fd, buffer, 1048576)&lt;1048576)
          return 1;
      while(n&gt;0) {
          Send(socket, buffer, n);
          read(fd, buffer, 1048576);
          n--;
      }
      RecvN(socket, &amp;hash, 4);
      printf("%d", ntohl(hash));
      return 0;
  }
  </code></pre>
  
  <p>The Send function is defined as:</p>
  
  <pre><code>void Send(Socket socket, void *buffer, int buflen) {
      int nleft, nsent;
      for(nleft=buflen; nleft&gt;0;) {
          if((nsent=send(socket-&gt;sockfd, buffer, nleft, 0))&lt;=0) {
              perror("Send error");
              exit(-1);
          } else {
              nleft-=nsent;
              buffer+=nsent;
          }
      }
  }
  </code></pre>
  
  <p>Where Socket is a pointer to a struct containing the socket file descriptor and others informations.</p>
  
  <p>I've tried to print nsent at each iteration, and I've discovered that the socket send function send only 1 byte at a time, so to transfer 1MB it takes lot of time.</p>
  
  <p>Which can be the problem?</p>