2

このコードを使用して、小さな HTTP サーバーにバイナリ ファイルを送信します

/* send binary data to client */

void send_binary(int sock_fd, char *file_name)
{

    int buff_size = 10240;
    char buff[buff_size];
    long file_size;
    FILE *pFile;
    size_t result;
    if ( (pFile = fopen(file_name, "rb")) == NULL){
        error("fopen error\n");
    }

    while( (result = fread(buff, 1, buff_size, pFile)) == buff_size){
        send(sock_fd, buff, buff_size, 0);
        buff[0] = '\0';
    }
    if (result > 0){
        if(feof(pFile)){
            send(sock_fd, buff, result, 0);
        }
        else{
            error("read error\n");
        }
    }
    fclose(pFile);

}

テキストでは機能しますが、jpeg ファイルでは機能しません。受信した画像ファイルが壊れています。

4

1 に答える 1