I am making my own simple webserver and I have gotten text files and html files to send perfectly, but when I try to send images, I am having some issues, and I can’t figure it out.
Here is what I have. I know sending one byte at a time is inefficient but it was for testing.
char buffer[1];
send_header(new_fd, get_file_type(file_location));
ifstream file;
file.open(temp.c_str(), ios::out | ios::binary);
while (file.good())
{
file.read(buffer, sizeof(buffer));
send(new_fd, buffer, strlen(buffer), 0);
}
Any ideas? Do I need to convert it to network byte order before I send?
Thanks!
strlen(buffer)is going to count up to the first null character ('\0'), which is a very common byte in image data. You need to, instead, figure out how many bytes were actually read (ifstream.read()does not supply this information) and use that in place of yourstrlencall.EDIT: You can obtain this with
ifstream.gcount()— so just replace yourstrlencall withfile.gcount()and everything should magically work.