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.
I already wrote server on C# for test purposes – and it works fine with my flex app.
C# code, which sends image:
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");
}
C++ code, which sends the same image:
void SocketServer::sendFile(SOCKET &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;
}
}
send method returns proper image size as sent value.
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
C# transfered result:

C++ transfered result:

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:
std::ifstream ifs("sphere.jpg");
std::string str((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
my string will be ~500 characters instead of 124Kbytes (~124k chars, image file size).
Here is C++ image result:

So I don’t really know why socket transfer only very small part of jpeg 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.
It may be due to the fact that you are not explicitly opening the file in binary mode. Try using: