Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
363 views
in Technique[技术] by (71.8m points)

network programming - problem sending image file over the socket in c

I am writing a C based server supporting HTTP. &buffer[5] has the file name. I set the header in next step and then send the file in block of 8Kb. It is working fine when I am requesting a txt file, but when I request a jpg file, it is not giving any o/p. Is there any specific care I need to take for image file or there is some other problem.

void web(int fd)
{
    int j, file_fd;
    int buflen; int len;

    long i, ret;
    char * fstr;
    static char buffer[BUFSIZE+1]; /* static so zero filled */



    ret =read(fd,buffer,BUFSIZE);   /* read Web request in one go */
    if(ret == 0 || ret == -1) { /* read failure stop now */
        //log1(SORRY,"failed to read browser request","",fd);
    }
    if(ret > 0 && ret < BUFSIZE)    /* return code is valid chars */
        buffer[ret]=0;      /* terminate the buffer */
    else buffer[0]=0;

    for(i=0;i<ret;i++)  /* remove CF and LF characters */
        if(buffer[i] == '
' || buffer[i] == '
')
            buffer[i]='*';
    log1(LOG,"request",buffer,1);

    if( strncmp(buffer,"GET ",4) && strncmp(buffer,"get ",4) )
        log1(SORRY,"Only simple GET operation supported",buffer,fd);

    for(i=4;i<BUFSIZE;i++) { /* null terminate after the second space to ignore extra stuff */
        if(buffer[i] == ' ') { /* string is "GET URL " +lots of other stuff */
            buffer[i] = 0;
            break;
        }
    }

    for(j=0;j<i-1;j++)  /* check for illegal parent directory use .. */
        if(buffer[j] == '.' && buffer[j+1] == '.')
            log1(SORRY,"HTTP/1.0 400 Bad Request: Invalid URI: 
 Parent directory (..) path names not supported",buffer,fd);

    if( !strncmp(&buffer[0],"GET /",6) || !strncmp(&buffer[0],"get /",6) ) /* convert no filename to index file */
        (void)strcpy(buffer,"GET /index.html");

    /* work out the file type and check we support it */
    buflen=strlen(buffer);
    fstr = (char *)0;
    for(i=0;extensions[i].ext != 0;i++) {
        len = strlen(extensions[i].ext);
                printf("%s%s%d
", &buffer[buflen-len],extensions[i].ext, strncmp(&buffer[buflen-len], extensions[i].ext, len));
        if( !strncmp(&buffer[buflen-len], extensions[i].ext, len)) {
            fstr =extensions[i].filetype;
            break;
        }
    }
    if(fstr == 0)
        log1(SORRY,"HTTP/1.0 400 Bad Request: Invalid URI:
 file extension type not supported",buffer,fd);
        printf("Buffer Value: %s
",&buffer[5]);
    if(( file_fd = open(&buffer[5],O_RDONLY)) == -1) /* open the file for reading */
               { 
        log1(SORRY, "HTTP/1.0 404 Not Found
 failed to open file",&buffer[5],fd);}
    printf("file Descrptr:%d
",file_fd);
    log1(LOG,"SEND",&buffer[5],1);

    (void)sprintf(buffer,"HTTP/1.0 200 OK
Content-Type: %s

", fstr);
    (void)write(fd,buffer,strlen(buffer));
        printf("Buffer Header:%s
",buffer);
        //int rec = read(file_fd, buffer, 10*BUFSIZE);
    /* send file in 8KB block - last block may be smaller */
    while ( (ret = read(file_fd, buffer, BUFSIZE)) > 0 ) {
        (void)write(fd,buffer,ret);
        printf("BufferData:%d
",strlen(buffer));
    }
#ifdef LINUX
    sleep(1);   /* to allow socket to drain */
#endif
    exit(1);
}

o/p for jpg file:

server: got connection from 127.0.0.1
Buffer Header:HTTP/1.0 200 OK
Content-Type: image/jpeg



BufferData:4
BufferData:95
BufferData:122
BufferData:24
BufferData:217

for txt file:

Buffer Value: config.txt
file Descrptr:4
Buffer Header:HTTP/1.0 200 OK
Content-Type: text/plain



BufferData:416
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If your code is working for txt files, give fread/fwrite a try instead of read/write . There might be a problem with NULL characters in jpg image which are not present in txt files. And do include a content length in header otherwise in some cases your browser will keep on requesting even if your download has finished. In simple words browser don't know when to stop.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...