开发者

Problem with c written server displaying a bmp image

First off, if anyone would like to compile the server and run it

  1. go to http://pastebin.com/qPnE3jV0
  2. compile and run it
  3. go to your browser and type http://localhost:7191/

The function I'm looking at particularly is:

void serveHTML (int socket) {

    char *message;
    int i;
    int j;

    //image data
    char bmpheader[54] = {0x42, 0x4D, 0x5A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, 0x13, 0x0B, 0x00, 0x00, 0x13, 0x0B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

    char bmpimagedata[36] = {0x07, 0x07, 0xFF, 0x07, 0x07, 0x07, 0x07, 0x07, 0xFF, 0xFF, 0x07, 0x07, 0x07, 0x07, 0x07, 0x66, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0xFF, 0xFF, 0xFF, 0x46, 0x00, 0x00};

   message = "HTTP/1.0 200 OK\r\n"
                "Content-Type: image/bmp\r\n"
                "Content-Length: 90\r\n";
   printf ("%s\n", message);
   write (socket, message, strlen(message));

   // now send the contents of the web page to be displayed
i=0;
j=0;
while (i < 54) {
    printf("%c", bmpheader[i]);
    write (socket, &bmpheader[i], 1);
    i++;
}
while (j < 36) {
    printf("%c", bmpimagedata[j]);
    write (socket, &bmpimagedata[j], 1);
    j++;
}


}
  • What I've done here is stored the hex data of a 3x3 bmp file into two arrays (for convenience of modification) the header and the image data. I try to get it to serve this bmp on a web browser. However it fails.

  • I've placed printf's all over the place to print what the server is spitting out... however I think I'm missing something (possibly a footer?).

Any 开发者_StackOverflow社区help would be appreciated, I'm pulling my hair out as to why it doesn't work.


Headers need to end with two \r\n - Append \r\n to your message constant.


A few points:

  1. Don't use such bizarre code to write out the arrays. Just use plain write(socket, bmpheader, sizeof bmpheader); and the same for the data.
  2. Inspect the return value of write(); it can fail.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜