我将在这里重述整个问题,以便它是可以回答的.
我能够在不使用套接字的同一台机器上完美地复制二进制文件,只是制作一个简单的复制功能.尝试实现此代码以复制到TCP/IP连接但无法使其工作.
FILE *filehandle = fopen("imagefile.jpg", "rb");
FILE *dest =fopen("imagecopy.jpg", "wb"); // copied image file
fseek(filehandle, 0, SEEK_END);
unsigned long filesize = ftell(filehandle);
char *buffer = (char*)malloc(sizeof(char)*filesize);
rewind(filehandle);
int bytesread = fread(buffer, sizeof(char), filesize, filehandle);
for( int i=0; i<filesize; i++ )
{
fputc(buffer[i], filehandle); // copies all the contents to dest
}
Run Code Online (Sandbox Code Playgroud)
上面的代码非常适合在计算机中复制图像文件,但是当实现在服务器上复制时,很难实现.
我正在尝试将图像文件从服务器发送到客户端,这两个文件都是用C语言手动创建的.服务器发送的文件长度只有在服务器发送文件时才知道,因此缓冲区是动态的在服务器中生成,如下所示:
服务器
fseek(filehandle, 0, SEEK_END);
long filesize = ftell(filehandle); // file could be 11000bytes
char *buffer = (char*)malloc(sizeof(char)*filesize); // char buffer with 11000 …Run Code Online (Sandbox Code Playgroud)