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

Discussion in 'C Programming' started by sree, Mar 10, 2005.

  1. sree

    sree Guest

    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.
    sree, Mar 10, 2005
    #1
    1. Advertising

  2. sree

    -berlin.de Guest

    sree <> wrote:
    > 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
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
    -berlin.de, Mar 10, 2005
    #2
    1. Advertising

  3. sree

    Eric Sosman Guest

    Re: how can i read a jpeg file using c and send back to the browserwhich is requesting

    sree wrote:
    > 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>

    --
    Eric Sosman, Mar 10, 2005
    #3
  4. sree

    T Koster Guest

    Re: how can i read a jpeg file using c and send back to the browserwhich is requesting

    sree wrote:
    > 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>
    T Koster, Mar 12, 2005
    #4
  5. sree

    sree Guest

    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.
    sree, Mar 13, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?TWFyaw==?=
    Replies:
    3
    Views:
    4,063
    Joerg Jooss
    Dec 30, 2003
  2. Joby
    Replies:
    0
    Views:
    1,617
  3. Replies:
    2
    Views:
    622
    Thomas Matthews
    Feb 27, 2007
  4. lovaspillando
    Replies:
    0
    Views:
    1,018
    lovaspillando
    Aug 26, 2007
  5. Ivan Alameda Carballo
    Replies:
    0
    Views:
    484
    Ivan Alameda Carballo
    Aug 26, 2007
Loading...

Share This Page