小编use*_*173的帖子

在C中发送图像文件

我使用此代码为我的小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);

}
Run Code Online (Sandbox Code Playgroud)

它适用于文本,但不适用于jpeg文件.收到的图像文件已损坏.

c sockets fopen send fread

1
推荐指数
1
解决办法
2993
查看次数

标签 统计

c ×1

fopen ×1

fread ×1

send ×1

sockets ×1