benG
benG

Reputation: 82

Cannot Send Image File (image/jpg) Using Winsock WSABUF

I'm stuck and I need help.

I'm trying to write the correct code for sending back an image file so the web browser can render it. It can send back text/html just fine, but image/* is not working.

You can see the code and the URL is shown below.

https://github.com/MagnusTiberius/iocphttpd/blob/master/iocphttpl/SocketCompletionPortServer.cpp

What the browser is receiving is just a few bytes of image data.

I tried vector, std::string and const char* to set the values of WSABUF, but still the same few bytes are sent over.

Please let know what is the missing piece to make this one work.

Thanks in advance.

Upvotes: 0

Views: 322

Answers (1)

Harry Johnston
Harry Johnston

Reputation: 36328

Here's your problem:

PerIoData->LPBuffer = _strdup(str.c_str());

The _strdup function only copies up until the first null, so it cannot be used to copy binary data. Consider using malloc and memcpy if you don't want to use the C++ library.

The alternate implementation (in the false branch) is also incorrect, because it saves the data in an object (vc) that goes out of scope before the I/O is completed. You could instead do something like

vector<char> * vc = new vector<char>;

Upvotes: 1

Related Questions