簡體   English   中英

通過C中的套接字發送圖像文件時出現問題

[英]problem sending image file over the socket in c

我正在寫一個支持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);
}

jpg文件的o / p:

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

您可能需要在HTTP標頭中指定正確的Content-Type。 它不再只是“文本”了。


HTTP響應行應該以CRLF結尾-請閱讀標准。 實際上,大多數Internet協議都要求該行結尾。

您尚未指定內容編碼; 您尚未指定數據長度。 我認為您應該兩者兼而有之。

為什么不將請求發送到Web服務器,該服務器可以正確處理小JPEG文件的請求,並查看其生成的內容。 您將檢查URL是否在瀏覽器中正常工作,並向您顯示圖像,然后使用低級程序(甚至可能是telnet web.server.example.com 80 )發送請求並收集響應。 將其與代碼生成的內容進行比較。

或者您可以閱讀標准。

您應該檢查寫入的字節數是否是您打算寫入的數字()。 如果寫得很短,則應再次嘗試編寫其余的內容-因此您需要圍繞寫的循環(或包含該循環的函數)。 如果向您報告了錯誤write()例如,通過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:

該請求是最小的; 響應可能不是,但是您必須嘗試發送的內容。

如果您的代碼適用於txt文件,請嘗試fread / fwrite而不是read / write。 jpg圖像中的NULL字符(txt文件中不存在)可能存在問題。 並在標頭中包含內容長度,否則在某些情況下,即使下載已完成,您的瀏覽器也會繼續請求。 簡單來說,瀏覽器不知道何時停止。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM