Saturday, 24 August 2013

How do I send a jpg image from my http server over to the browser?

How do I send a jpg image from my http server over to the browser?

I'm having trouble getting the jpg image to display in the browser. I'm
using "localhost" as my test client in my browser to my web server. The
image is located on my desktop in a folder called "G". The Web Server
program is in the same folder "G". And the index.html file is also in the
same folder "G" on my desktop. In the index.html file there is a html code
block that reads "", referring to the image in the folder "G". However,
The jpg image won't display... I don't know why. Here are the codes to the
web server:
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;
}

No comments:

Post a Comment