title: Sending an image from a C# client to a C server
body: <p>If I send plain text there is no problem. Everything is ok.
  However If I try to send from the C# client an image, the server receives correct bytes number, but when I save the buffer to a file (in binary mode - wb), it always has 4 bytes.</p>
  
  <p>I send it by the C# client by using the function <code>File.ReadAllBytes()</code>.</p>
  
  <p>My saving code looks like</p>
  
  <pre><code>   FILE * pFile;
     char *buf = ReceiveMessage(s);
     pFile = fopen (fileName , "wb");
     fwrite(buf, sizeof(buf[0]), sizeof(buf)/sizeof(buf[0]), pFile);
     fclose (pFile);
     free(buf);
  </code></pre>
  
  <p>My receiving function looks like</p>
  
  <pre><code>static unsigned char *ReceiveMessage(int s)
  {
     int prefix;
     recv(s, &amp;prefix, 4, 0);
     int len = prefix;
     char *buffer= (char*)malloc(len + 1);
     int received = 0, totalReceived = 0;
     buffer[len] = '\0';
     while (totalReceived &lt; len)
     {
        if (len - totalReceived &gt; BUFFER_SIZE)
        {
           received = recv(s, buffer + totalReceived, BUFFER_SIZE, 0);
        }
        else
        {
           received = recv(s, buffer + totalReceived, len - totalReceived, 0);
        }
        totalReceived += received;
     }
  
     return buffer;
  }
  </code></pre>