【问题标题】:problem sending image file over the socket in c在c中通过套接字发送图像文件的问题
【发布时间】:2011-01-25 16:43:23
【问题描述】:

我正在编写一个支持 HTTP 的基于 C 的服务器。 &buffer[5] 具有文件名。我在下一步设置标题,然后以 8Kb 的块发送文件。当我请求一个 txt 文件时它工作正常,但是当我请求一个 jpg 文件时,它没有提供任何 o/p。 我需要对图像文件采取什么特别的措施还是有其他问题。

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] == '\r' || buffer[i] == '\n')
            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: \n Parent directory (..) path names not supported",buffer,fd);

    if( !strncmp(&buffer[0],"GET /\0",6) || !strncmp(&buffer[0],"get /\0",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\t%s\t%d\n", &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:\n file extension type not supported",buffer,fd);
        printf("Buffer Value: %s\n",&buffer[5]);
    if(( file_fd = open(&buffer[5],O_RDONLY)) == -1) /* open the file for reading */
               { 
        log1(SORRY, "HTTP/1.0 404 Not Found\n failed to open file",&buffer[5],fd);}
    printf("file Descrptr:%d\n",file_fd);
    log1(LOG,"SEND",&buffer[5],1);

    (void)sprintf(buffer,"HTTP/1.0 200 OK\r\nContent-Type: %s\r\n\r\n", fstr);
    (void)write(fd,buffer,strlen(buffer));
        printf("Buffer Header:%s\n",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\n",strlen(buffer));
    }
#ifdef LINUX
    sleep(1);   /* to allow socket to drain */
#endif
    exit(1);
}

o/p 用于 jpg 文件:

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

对于txt文件:

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



BufferData:416

【问题讨论】:

    标签: c network-programming


    【解决方案1】:

    如果您的代码适用于 txt 文件,请尝试使用 fread/fwrite 而不是 read/write 。 txt 文件中不存在的 jpg 图像中的 NULL 字符可能存在问题。 并且在标题中包含内容长度,否则在某些情况下,即使您的下载完成,您的浏览器也会继续请求。简而言之,浏览器不知道何时停止。

    【讨论】:

      【解决方案2】:

      您可能需要在 HTTP 标头中指定正确的 Content-Type。它不再只是“文本”。


      HTTP 响应行应该以 CRLF 结尾 - 阅读标准。事实上,大多数互联网协议都需要此行结尾。

      您尚未指定 Content-Encoding;你没有指定数据的长度。我认为你应该两者都做。

      为什么不向正确处理小型 JPEG 文件请求的 Web 服务器发送请求,然后看看它会生成什么。您将检查 URL 在浏览器中是否有效,向您显示图像,然后您将使用低级程序(甚至可能是 telnet web.server.example.com 80)发送请求并收集响应。将其与您的代码生成的内容进行比较。

      或者你可以阅读标准。

      您应该检查写入的字节数是否是您打算写入的数字()。如果你得到一个简短的写,你应该再次尝试写剩下的——所以你需要一个围绕你的写的循环(或一个包含循环的函数)。如果向您报告了错误(例如,write()),您也应该报告错误。


      这是一个向特定服务器发送请求并收集 368 字节 gif 的 shell 脚本:

      {
      echo "GET /img/mailtruck.gif HTTP/1.0^M"
      echo "^M"
      } |
      tee request |
      nc my.earthlink.net 80
      

      '^M'字符是回车;我使用 CONTROL-VCONTROL-M 输入这些。程序nc (netcat) 将请求发送到服务器。响应以以下开头:

      HTTP/1.1 200 OK
      Cache-Control: Tue, 25 Jan 2011 16:34:03 GMT
      ETag: W/"368-1219455793000"
      Last-Modified: Sat, 23 Aug 2008 01:43:13 GMT
      Content-Type: image/gif
      Content-Length: 368
      Date: Tue, 25 Jan 2011 08:34:03 GMT
      Server: Apache-Coyote/1.1
      Connection: Keep-Alive
      

      十六进制转储格式的完整响应是:

      0x0000: 48 54 54 50 2F 31 2E 31 20 32 30 30 20 4F 4B 0D   HTTP/1.1 200 OK.
      0x0010: 0A 43 61 63 68 65 2D 43 6F 6E 74 72 6F 6C 3A 20   .Cache-Control: 
      0x0020: 54 75 65 2C 20 32 35 20 4A 61 6E 20 32 30 31 31   Tue, 25 Jan 2011
      0x0030: 20 31 36 3A 33 32 3A 35 39 20 47 4D 54 0D 0A 45    16:32:59 GMT..E
      0x0040: 54 61 67 3A 20 57 2F 22 33 36 38 2D 31 32 31 39   Tag: W/"368-1219
      0x0050: 34 35 35 37 39 33 30 30 30 22 0D 0A 4C 61 73 74   455793000"..Last
      0x0060: 2D 4D 6F 64 69 66 69 65 64 3A 20 53 61 74 2C 20   -Modified: Sat, 
      0x0070: 32 33 20 41 75 67 20 32 30 30 38 20 30 31 3A 34   23 Aug 2008 01:4
      0x0080: 33 3A 31 33 20 47 4D 54 0D 0A 43 6F 6E 74 65 6E   3:13 GMT..Conten
      0x0090: 74 2D 54 79 70 65 3A 20 69 6D 61 67 65 2F 67 69   t-Type: image/gi
      0x00A0: 66 0D 0A 43 6F 6E 74 65 6E 74 2D 4C 65 6E 67 74   f..Content-Lengt
      0x00B0: 68 3A 20 33 36 38 0D 0A 44 61 74 65 3A 20 54 75   h: 368..Date: Tu
      0x00C0: 65 2C 20 32 35 20 4A 61 6E 20 32 30 31 31 20 30   e, 25 Jan 2011 0
      0x00D0: 38 3A 33 32 3A 35 39 20 47 4D 54 0D 0A 53 65 72   8:32:59 GMT..Ser
      0x00E0: 76 65 72 3A 20 41 70 61 63 68 65 2D 43 6F 79 6F   ver: Apache-Coyo
      0x00F0: 74 65 2F 31 2E 31 0D 0A 43 6F 6E 6E 65 63 74 69   te/1.1..Connecti
      0x0100: 6F 6E 3A 20 4B 65 65 70 2D 41 6C 69 76 65 0D 0A   on: Keep-Alive..
      0x0110: 0D 0A 47 49 46 38 39 61 25 00 21 00 B3 08 00 FD   ..GIF89a%.!.....
      0x0120: 9C 00 DE 36 03 58 44 29 19 15 04 D2 D3 D3 F1 85   ...6.XD)........
      0x0130: 66 A4 A4 A3 FE FE FE FE FE FE 00 00 00 00 00 00   f...............
      0x0140: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 21   ...............!
      0x0150: F9 04 01 00 00 08 00 2C 00 00 00 00 25 00 21 00   .......,....%.!.
      0x0160: 00 04 FF 10 C9 49 AB BD 38 EB 49 84 27 5B 78 19   .....I..8.I.'[x.
      0x0170: 1E 60 7A 86 28 76 82 E9 9E 02 A8 8E E5 6B 03 E8   .`z.(v.......k..
      0x0180: CC D5 77 FF A9 A4 56 6F E8 12 84 58 C4 E4 69 43   ..w...Vo...X..iC
      0x0190: 52 26 03 81 C1 A5 83 20 0C 9C 43 28 54 6A B1 22   R&..... ..C(Tj."
      0x01A0: 3C 03 21 56 5B 28 6C A7 03 EB 60 CD CB 06 08 E5   <.!V[(l...`.....
      0x01B0: F2 B9 0B 5E B3 C3 6E F3 5B 1F CD D8 FF 6C 62 00   ...^..n.[....lb.
      0x01C0: 64 7A 70 5A 5C 16 02 77 80 61 2D 01 02 5A 04 01   dzpZ\..w.a-..Z..
      0x01D0: 07 01 72 73 15 4D 51 8A 8C 26 6B 50 66 72 96 7D   ..rs.MQ..&kPfr.}
      0x01E0: 89 2F 60 9B 6B 38 9E 7B 7A 83 87 74 37 8B 2D 9B   ./`.k8.{z..t7.-.
      0x01F0: 50 86 50 AD 97 13 82 36 9B A9 61 B4 7C 66 A7 32   P.P....6..a.|f.2
      0x0200: 12 4D 44 01 83 76 9F 96 7A 8D 6B C3 06 C7 4A 51   .MD..v..z.k...JQ
      0x0210: 5A 95 05 70 7C 03 06 24 69 13 DA 04 4F D4 BF 95   Z..p|..$i...O...
      0x0220: 87 69 DE 14 DA 41 6E E1 E1 1E ED 31 98 50 EE 36   .i...An....1.P.6
      0x0230: EB EC EF 55 06 C3 12 56 05 00 05 EE 62 D4 20 1D   ...U...V....b. .
      0x0240: B2 A7 A1 83 36 13 FE 3E 48 22 37 40 53 BE 0D 2C   ....6..>H"7@S..,
      0x0250: B4 F1 F3 67 80 8F 22 16 0F 81 A0 30 60 E2 98 16   ...g.."....0`...
      0x0260: 45 07 1D 8C E8 A0 23 40 E2 20 61 22 47 5A 08 82   E.....#@. a"GZ..
      0x0270: 2E 46 44 95 19 58 94 84 39 43 1B CD 9B 30 23 00   .FD..X..9C...0#.
      0x0280: 00 3B                                             .;
      0x0282:
      

      请求是最小的;响应可能不是,但是您必须尝试发送的内容。

      【讨论】:

      • @Jonathan...正如您在 o/p 上看到的那样,我打印的 HTTP 标头看起来不错。我只遇到了 .jpg 文件的问题......如果我将 Buffsize 从 8Kb 增加到更高的值,是否有任何范围。
      • 如果您愿意,可以使用 URL sstatic.net/stackoverflow/img/apple-touch-icon.png
      • 伙计们非常感谢您的评论和回答......这真的帮助了我很多。有两个重要问题是: 1. 没有给出内容长度...我使用 stat.h 并将内容长度放在标题中。 2. Content-Type 格式有问题。当我从配置文件中读取类型时,我没有注意删除 '\n' ,并且该格式与 HTTP std 不匹配............再次感谢您向我指出这些事情......:)
      猜你喜欢
      • 2013-03-24
      • 1970-01-01
      • 2012-08-17
      • 2013-12-17
      • 1970-01-01
      • 2013-12-31
      • 2012-01-30
      相关资源
      最近更新 更多