title: Send file over SOCKET
body: <p>I'm creating simple Socket server. Flex application will be client for this server. On special request I need to transfer image file (jpeg) from server to client over socket.</p>
  
  <p>I already wrote server on C# for test purposes - and it works fine with my flex app.</p>
  
  <p>C# code, which sends image:</p>
  
  <pre><code>private void sendImage(Socket client)
          {
              Bitmap data = new Bitmap("assets/sphere.jpg");
              Image img = data;
              MemoryStream ms = new MemoryStream();
              img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
              byte[] buffer = ms.ToArray();
              sendInt(client, buffer.Length);
              client.Send(buffer);
              Console.WriteLine("Image sent");
          }
  </code></pre>
  
  <p>C++ code, which sends the same image:</p>
  
  <pre><code>void SocketServer::sendFile(SOCKET &amp;client, std::string filename)
  {
      std::ifstream file (filename, std::ios::ate);
      if (file.is_open())
      {
          std::ifstream::pos_type size = file.tellg();
          char * memblock = new char [size];
          file.seekg (0, std::ios::beg);
          file.read (memblock, size);
          file.close();
          sendInt(client, size);
          send(client, memblock, size, 0);
          delete[] memblock;
      }
  }
  </code></pre>
  
  <p><code>send</code> method returns proper image size as sent value.</p>
  
  <p>For some reason I can't debug in Adobe Flash Builder 4.6 on Windows 8, so I created output widget where I can view transfer result as string</p>
  
  <p>C# transfered result:
  <img src="https://i.stack.imgur.com/AUB80.png" alt="enter image description here"></p>
  
  <p>C++ transfered result:
  <img src="https://i.stack.imgur.com/0H3sb.png" alt="enter image description here"></p>
  
  <p>As you can see 500 or so first characters are the same. The rest of C++ one are those 'i' symbols. The strange thing is that if I read file into string using this code for example:</p>
  
  <pre><code>std::ifstream ifs("sphere.jpg");
  std::string str((std::istreambuf_iterator&lt;char&gt;(ifs)), std::istreambuf_iterator&lt;char&gt;());
  </code></pre>
  
  <p>my string will be ~500 characters instead of 124Kbytes (~124k chars, image file size).</p>
  
  <p>Here is C++ image result:
  <img src="https://i.stack.imgur.com/BIp9a.png" alt="enter image description here"></p>
  
  <p>So I don't really know why socket transfer only very small part of <code>jpeg</code> correctly and the rest is wrong? As I mentioned - there is no problems if I transfer byte array from C# to Flex, so I assume that everything fine on Flex side.</p>