首页 > 解决方案 > fwrite() 没有完成写入文件

问题描述

试图将文件从客户端发送到服务器。

这是我收到它的方式:

printf("Server receiving file...\n");
while((bytes_read = read(sd, buf, sizeof(buf))) > 0){ //second read now retrieving the file size
  printf("writing");
  fwrite(buf, 1, bytes_read, fp);
  printf("checking if reached here");
}

出于某种原因,第二个打印语句“检查是否到达这里”永远不会到达,并且程序只是没有退出循环。可能是什么问题?

PS。我的客户发送所有字节,因此发送没有问题。它只是写入文件

标签: cbytefwritefread

解决方案


套接字是否阻塞(默认)?

如果是,则只要没有数据可读取,read() 就会挂起,等待更多数据(或等待套接字关闭,在这种情况下它将返回 0 字节)。


推荐阅读