spe spe - 3 years ago 309
HTTP Question

Send binary file in HTTP response using C sockets

I`m trying to send a binary file (png image) in http response.

FILE *file;
char *buffer;
int fileLen;

//Open file
file = fopen("1.png", "rb");
if (!file)

//Get file length
fseek(file, 0, SEEK_END);
fseek(file, 0, SEEK_SET);

//Allocate memory
buffer=(char *)malloc(fileLen+1);
if (!buffer)
fprintf(stderr, "Memory error!");

//Read file contents into buffer
fread(buffer, fileLen, 1, file);

char header[102400];

"HTTP/1.1 200 OK\n"
"Date: Thu, 19 Feb 2009 12:27:04 GMT\n"
"Server: Apache/2.2.3\n"
"Last-Modified: Wed, 18 Jun 2003 16:05:58 GMT\n"
"ETag: \"56d-9989200-1132c580\"\n"
"Content-Type: image/png\n"
"Content-Length: %i\n"
"Accept-Ranges: bytes\n"
"Connection: close\n"
"\n", fileLen);

char *reply = (char*)malloc(strlen(header)+fileLen);
strcpy(reply, header);
strcat(reply, buffer);

printf("msg %s\n", reply);

//return 0;
int sd = socket(PF_INET, SOCK_STREAM, 0);

struct sockaddr_in addr;
bzero(&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(8081);
addr.sin_addr.s_addr = INADDR_ANY;

printf("bind error\n");

if (listen(sd, 16)!=0)
printf("listen error\n");

int size = sizeof(addr);
int client = accept(sd, &addr, &size);

if (client > 0)
printf("client connected\n");
send(client, reply, strlen(reply), 0);

but my browser does not understand this =( What am I doing wrong exactly?

UPD: I tried to send text data - its OK. But binary data fails

Answer Source

The problem is that your message body is being treated as a null-terminated text string (you use strcat and strlen on it), when it isn't one: it is binary data (a PNG file). Therefore, strcat and strlen both stop on the first 0 byte in the image (typically quite early).

Your program is even printing out the response body: notice that it gives the correct header, but that once the PNG header (binary data) starts, there is only a few bytes.

  1. The line strcat(reply, buffer), where buffer potentially contains 0 bytes. Change it to memcpy(reply+strlen(header), buffer, fileLen).
  2. The line send(client, reply, strlen(reply), 0), where reply potentially contains 0 bytes. Pre-calculate the length of the reply, or replace the strlen with strlen(header)+fileLen.

Another bug is that you aren't closing the connection when you're done, so the browser will just wait forever. You need this, after send:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download