c 套接字发送文件大小,然后发送文件

标签 c sockets sendfile

这是client.c的片段:

size_t fsize;
int total_bytes_read = 0, ret_val;

hp = gethostbyname(argv[1]);
DescrittoreClient = socket(AF_INET, SOCK_STREAM, 0);
    connect(DescrittoreClient, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
strcpy(Buffer, filename);
send(DescrittoreClient, Buffer, strlen(Buffer), 0);
ret_val = read(DescrittoreClient, &fsize, sizeof(fsize));
if(ret_val == -1){
    printf("Errore durante ricezione grandezza file\n");
    close(DescrittoreClient);
    exit(1);
}

fd = open(filename, O_CREAT | O_WRONLY,0644);
if (fd  < 0) {
    perror("open");
    exit(1);
}

while(total_bytes_read < fsize){
    while ((nread = read(DescrittoreClient, Buffer, sizeof(Buffer))) > 0){
        write(fd, Buffer, nread);
        total_bytes_read += nread;
    }
}
printf("File ricevuto\n");

这是 server.c 的一个片段:

int DescrittoreServer, DescrittoreClient, LunghezzaClient;
int NumPorta = atoi(argv[1]);
char Buffer[1024] = {};
int rc, fd;
off_t offset = 0;
struct stat stat_buf;
char filename[1024] = {};
int fsize[10240] = {};

DescrittoreServer = socket(AF_INET, SOCK_STREAM, 0);

if(bind(DescrittoreServer, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0){
    perror("Errore di bind\n");
    close(DescrittoreServer);
    exit(1);
}
listen(DescrittoreServer, 5);
LunghezzaClient = sizeof(cli_addr);
while(1){
    DescrittoreClient = accept(DescrittoreServer, (struct sockaddr *) &cli_addr, &LunghezzaClient);

rc = recv(DescrittoreClient, filename, sizeof(filename), 0);

fprintf(stderr, "Ricevuta richiesta di inviare il file: '%s'\n", filename);

/* open the file to be sent */
fd = open(filename, O_RDONLY);

/* get the size of the file to be sent */
fstat(fd, &stat_buf);
*fsize = stat_buf.st_size;
send(DescrittoreClient, fsize, sizeof(fsize), 0);

/* copy file using sendfile */
offset = 0;
rc = sendfile(DescrittoreClient, fd, &offset, stat_buf.st_size);
if (rc != stat_buf.st_size) {
    fprintf(stderr, "Trasferimento incompleto: %d di %d bytes\n", rc, (int)stat_buf.st_size);
    exit(1);
}   

    close(DescrittoreClient);
    close(fd);
}

这段代码做了什么:
1) 客户端发送文件名
2)服务器发送文件的长度(以字节为单位)
3) 客户端收到文件的长度
4) 服务器发送文件
5) 客户端收到文件
问题是文件没有完全被接收,客户端只写入了其中的一部分(例如,服务器发送了一个2143字节的文件,客户端只接收了95字节) 我不明白为什么!
PS:一些错误处理已被删除,因此代码更具可读性!

最佳答案

你必须在循环中调用 recv() 并总结它的返回值:

while (read_bytes < filesize) {
    read_bytes += recv(socket, buffer + read_bytes, filesize-read_bytes, flags)
}

当然你还应该添加错误处理,即检查你是否读取了 0 个字节并且连接已关闭。

关于c 套接字发送文件大小,然后发送文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11192816/

相关文章:

c 发送和接收文件

c - C 头文件中的函数?

C/Linux - 服务器 <-> 与命名管道的终端通信

你能解析带有 `scanf` 的句子并检测换行符吗?

c - select 等待在套接字中写入的奇怪行为

ruby-on-rails - rails 中的 SSL 网站将我转发到带有端口 443 后缀的 url

c++ - 使用 sendfile() 通过线程或其他高效的复制文件方法复制文件

c - 我的老师做了这个功能,但我不明白它有什么用

java - 通过 Android 上的 TCP 套接字流式传输音频

sockets - 无法修复错误10038 WSAENOTSOCK