加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
recvFile.c 1.19 KB
一键复制 编辑 原始数据 按行查看 历史
htkd 提交于 2024-06-18 21:20 . bp transfer done
#include "client.h"
int recvFile(int sockfd, int len){
// 接收文件名
char fileName[128] = { 0 };
recvn(sockfd, fileName, len);
// 接收(剩余)文件大小
off_t leftSize;
recvn(sockfd, &leftSize, sizeof(leftSize));
printf("leftSize = %ld\n", leftSize);
//int fd = open(fileName, O_RDWR | O_APPEND | O_CREAT, 0644);
int fd = open(fileName, O_RDWR | O_CREAT, 0644);
if (fd == -1){
close(sockfd);
error(1, errno, "open");
}
off_t offset = lseek(fd, 0, SEEK_END);
off_t fileSize = offset + leftSize;
int pipefds[2];
pipe(pipefds);
off_t slice = fileSize / 100;
off_t currSize = offset;
off_t lastSize = offset;
while (currSize < fileSize){
int bytes = splice(sockfd, NULL, pipefds[1], NULL, 4096, SPLICE_F_MORE);
if (bytes == 0) break;
bytes = splice(pipefds[0], NULL, fd, NULL, bytes, SPLICE_F_MORE);
currSize += bytes;
if (currSize - lastSize >= slice){
printf("recv %5.2lf%%\r", (double)currSize / fileSize * 100);
fflush(stdout);
lastSize = currSize;
}
}
printf("recv 100.00%%\n\n");
close(fd);
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化