首页 > 解决方案 > 使用 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)效果很好)

标签: cwindowssocketswinsock

解决方案


推荐阅读