how can i read a jpeg file using c and send back to the browser which is requesting

S

sree

i am doing project a simple http server. it is being writen in
c using sockets. so when there is a request to read a jpeg or any other
file icant do it. my code is working only for html and txt fils. Is
there seperate way to open jpegs etc and buffer them and send them.
 
J

Jens.Toerring

sree said:
i am doing project a simple http server. it is being writen in
c using sockets. so when there is a request to read a jpeg or any other
file icant do it. my code is working only for html and txt fils. Is
there seperate way to open jpegs etc and buffer them and send them.

In principle there should be no problem in opening, reading or
writing jpeg (or other non-text) files. The only thing you may
have to be careful about is to open the file in binary mode in-
stead of the default text mode, e.g. to open the file for rea-
ding use

FILE *fp = fopen( "myfile.jpeg", "rb" );

since otherwise on some platforms "\r\n" combinations might not
get read in in the way you need it.

And, of course using text oriented functions like fscanf() or
fprintf() etc. doesn't make sense with these files, use fread()
and fwrite(). If that shouldn't help you solve your problem you
should post the relevant part of your code since it otherwise
is impossible to guess what might be going wrong. But keep out
stuff about sockets since that is too much dependent on the
platform you're using and not part of the C language, so you
better ask about that in a newsgroup that is for discussions
about programming on your platform.

Regards, Jens
 
E

Eric Sosman

sree said:
i am doing project a simple http server. it is being writen in
c using sockets. so when there is a request to read a jpeg or any other
file icant do it. my code is working only for html and txt fils. Is
there seperate way to open jpegs etc and buffer them and send them.

<divination>

You have an error at line 362.

<guess>

Open the JPEG file with "rb" mode instead of just "r".

</guess>

</divination>
 
T

T Koster

sree said:
i am doing project a simple http server. it is being writen in
c using sockets. so when there is a request to read a jpeg or any other
file icant do it. my code is working only for html and txt fils. Is
there seperate way to open jpegs etc and buffer them and send them.

Jens and Eric have already given you likely C-related answers, but since
you haven't told us exactly what is broken, there are other (off-topic)
possibilities:

<OT>
When you say your code is "working" only for html and txt files, do you
mean that jpeg files are not sent across the to the client at all? Or
do you get unexpected output? If you expect a to see a picture showing
up in your browser but instead you get seemingly garbled nonsensical
text, the problem may not be in your file I/O but with your handling of
the HTTP protocol, such as not sending the correct Content-Type headers
for example.
</OT>
 
S

sree

thanks to jens, Eric and Koster for helping me.

The mistake i did is for jpeg files to send we have to send the http
respond header Content-type settin it as image/jpeg. which i did not
do.
it is not mistake in c code but sending the header.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top