我已经创建了服务器和客户端进行通信。客户端发送图像的二进制数据,然后服务器接收它并写入文件。我在下面粘贴了必要的代码。
std::stringstream binStr;
bytes_received = recv(new_sd, &binStr, sizeof(binStr) ,0);
std::cout << binStr << std::endl;
char buff[1024*1024];
std::string image;
while (!binStr.eof())
{
binStr.read(buff, sizeof (buff));
image.append(buff, binStr.gcount());
}
int id = 1;
std::stringstream ss2;
ss2 << id;
std::string str2 = ss2.str();
std::ofstream img(str2.c_str(),std::ios::binary);
std::cout << image.c_str() << std::endl;
img.write(image.c_str(), image.length());
此代码创建名称为 id 的文件,但它是一个空文件。我该如何解决?