c - 使用 C 套接字编程下载文件
问题描述
我需要使用套接字传输 .txt 文件。我试试这个:
服务器:
#define CHUNKSIZE 1024
FILE* fd = fopen("download.txt", "rb");
size_t rret, wret;
int bytes_read;
char buffer[CHUNKSIZE];
while (!feof(fd)) {
if ((bytes_read = fread(&buffer, 1, sizeof(buffer), fd)) > 0)
send(Connections[index], buffer, bytes_read, 0);
else
break;
}
fclose(fd);
客户:
char text[1024];
size_t datasize;
FILE* fd = fopen("download.txt", "wb");
while (true)
{
datasize = recv(Connection, (char*)&text, sizeof(text), NULL);
fwrite(&text, 1, datasize, fd);
}
fclose(fd);
但是客户端没有写任何东西到download.txt 可能是什么问题?(我没有粘贴完整的代码,因为发送消息(recv,send)效果很好)
解决方案
推荐阅读
- python - 使用 tkinter 可执行文件制作 python 项目
- html - 标签中的链接正在更改
- c - 如何修复在 C 中使用 struct dirent 时的分段错误
- node.js - 如何解释 nodejs repl 自定义评估函数签名?
- python - 如何使用 Django 将变量从视图传递到基本 html 模板
- c# - 获取无效 SSL 证书 (.NET) 的详细说明
- r - 替换字符串中的非重复字符
- java - 基于 Log4j2 java 属性的配置不删除旧文件
- php - 如何将 SQLserver 2014 连接到 Laravel 应用程序
- r - 列可以是向量或列表类吗?