如何从我的http服务器向浏览器发送jpg图像?

时间:2013-08-25 02:46:40

标签: http

我无法在浏览器中显示jpg图像。我在我的浏览器中使用“localhost”作为我的测试客户端到我的Web服务器。该图像位于我的桌面上名为“G”的文件夹中。 Web Server程序位于同一文件夹“G”中。 index.html文件也在我桌面上的同一文件夹“G”中。在index.html文件中有一个html代码块,它读取<img src='background.jpg'>,指的是文件夹“G”中的图像。但是,jpg图像不会显示......我不知道为什么。以下是Web服务器的代码:

<!-- language: lang-c -->

DWORD WINAPI onConnect( LPVOID lpParam )
{
int sent = 0, received = 0;
winsocketT * ws = (struct winsocketT*)lpParam;
SOCKET connectfd = *ws->connectfd;

//closesocket(*ws->listenfd);

//must receive opening message from client (or else server cannot send, and client cannot receive.
char * recvbuf;
recvbuf = (char*)calloc(BUFSIZ * 4, sizeof(char));
recv(connectfd, recvbuf, BUFSIZ * 4, 0);//block until opening message received  
printf("\n%d%s%d\n",CountMe,recvbuf,CountMe);
//printf("\n%s\n", recvbuf);//will not print on this thread.
char* contentlengthpattern = "(Content-Length:)[[:s:]]([[:d:]]+)";
match_results<const char*> m;
tr1::regex rx;
string contentLength;
//match Sec-WebSocket-Key1 
m = match_results<const char*>();
rx = tr1::regex(contentlengthpattern);
tr1::regex_search(recvbuf, m, rx);
contentLength = m[2];       
int contentlength = atoi(contentLength.c_str());    

//if any form data exists, add its length (in bytes) to 'received':
char* formdatapattern = "(\r\n\r\n)(.+)";
string formdata;
m = match_results<const char*>();
rx = tr1::regex(formdatapattern);
tr1::regex_search(recvbuf, m, rx);
formdata = m[2];
received = formdata.length();

//continue receiving until we've received Content-Length bytes.
while (received > 0 && received < contentlength)
{                                   
    recvbuf = (char*)calloc(BUFSIZ * 4, sizeof(char));
    received += recv(*ws->connectfd, recvbuf, BUFSIZ, 0);//blocks
    printf("Received %d bytes.\n\n%s\n", received,  recvbuf);
}

char bufferx[1024];

char *msg;

FILE *fp;

if(CountMe == 1){


if((fp = fopen("index.html", "r")) == NULL){
    printf("unable to open file.\n");

    return -1;
}



msg = "HTTP/1.1 200 OK\r\n"
            "Server: skyezine \n"
            "Content-Type: text/html\r\n";


send(connectfd, msg, strlen(msg), 0);

fgets(bufferx, sizeof(bufferx), fp);

while(!feof(fp)){

    send(connectfd, bufferx, strlen(bufferx), 0);

    fgets(bufferx, sizeof(bufferx), fp);

}

    CountMe++;
}//if

else{

    if((fp = fopen("background.jpg", "rb")) == NULL){
        printf("unable to open file.\n");

        return -1;
    }

    int filelength;

    char FileLength[1024];

    fseek(fp, 0, SEEK_END);


    filelength = ftell(fp);

    itoa(filelength, FileLength, 10);

    char *msg2 = FileLength;

    char *msg3 = "\n\r\n\r\n";

    printf("length:---->%s<-----\n", msg2);

    msg = "HTTP/1.1 200 OK\r\n"
            "Server: skyezine \n"
            "Content-Type: image/jpeg\n"
            "Content-Length: ";

    printf("response-------->%s", msg);

    send(connectfd, msg, strlen(msg), 0);

    send(connectfd, msg2, strlen(msg2), 0);

    send(connectfd, msg3, strlen(msg3), 0);


    fgets(bufferx, sizeof(bufferx), fp);

    printf("---->");
    while(!feof(fp)){

        send(connectfd, bufferx, strlen(bufferx), 0);

        fgets(bufferx, sizeof(bufferx), fp);

        printf("%s",bufferx);
    }

    printf("<-----------");
}
fseek(fp, 0L, SEEK_END);

int size = ftell(fp);

printf("***size:%d length:%d***\n", size, strlen(bufferx));

closesocket(connectfd);
return 0;
}

0 个答案:

没有答案