在C中的进程之间传输JPEG文件
我正在用客户端编写服务器,但在传输图像时遇到问题。我试图在C中的进程之间传输JPEG文件,c,image,jpeg,fwrite,fread,C,Image,Jpeg,Fwrite,Fread,我正在用客户端编写服务器,但在传输图像时遇到问题。我试图fread缓冲区中的文件,将缓冲区发送到客户端,并fwrite在新文件中写入缓冲区,结果成功了!现在它不再工作了,我不知道为什么,当我检查新文件时,我的图像打开器说无法打开JPEG文件等。 我的阅读代码: int fd=open(pathname, O_RDONLY, 0666); struct stat fileStat; if(fstat(fd,&fileStat) < 0) { perror("\nfil
fread
缓冲区中的文件,将缓冲区发送到客户端,并fwrite
在新文件中写入缓冲区,结果成功了!现在它不再工作了,我不知道为什么,当我检查新文件时,我的图像打开器说无法打开JPEG文件等。我的阅读代码:
int fd=open(pathname, O_RDONLY, 0666);
struct stat fileStat;
if(fstat(fd,&fileStat) < 0)
{
perror("\nfilestat\n");
exit(-1);
}
int fileSize=(int)fileStat.st_size;
//printf("File Size: \t\t%d bytes\n",fileSize);
FILE *f=fdopen(fd, "rb");
if(f==NULL)
{
perror("\nfopen\n");
exit(-1);
}
void *buf=malloc(fileSize);
if(buf==NULL)
{
perror("\nmalloc\n");
return -1;
}
int FR=fread(buf, fileSize, 1, f);
if(FR<0)
{
perror("\nfread\n");
exit(-1);
}
客户端从收到的消息中提取后,我将其写入一个新文件:
char *clientfile="./qui.jpg";
int fdbis=open(clientfile, O_WRONLY|O_CREAT|O_TRUNC, 0666);
if(fdbis<=0)
{
perror("\nopen 2\n");
exit(-1);
}
FILE *fbis=fdopen(fdbis, "wb");
if(fbis==NULL)
{
perror("fdopen");
exit(-1);
}
int WR=fwrite((void*)buf, strlen(buf), 1, fbis);
if(WR<=0)
{
perror("\nfwrite\n");
exit(-1);
}
char*clientfile=“./qui.jpg”;
int fdbis=open(clientfile,O|u WRONLY | O|u CREAT | O|u TRUNC,0666);
如果(fdbis因为jpeg文件是二进制文件,所以不能使用字符串函数对其进行操作。
strlen(buf)
将返回长度,直到第一个空终止
使用memcpy
因为jpeg文件是二进制文件,所以不能使用字符串函数对其进行操作。
strlen(buf)
将返回长度,直到第一个空终止
使用memcpy
buf
中的数据不是有效的C字符串。因此,您不能在buf
上使用sprintf
或strlen
。buf
中的数据不是有效的C字符串。因此您不能在buf
上使用sprintf
或strlen现在它不再运行了,似乎写入的字节数比需要和请求的要少。我不久前切换到memcpy,它工作了,现在它不再运行了,似乎写入的字节数比需要和请求的要少。
char *clientfile="./qui.jpg";
int fdbis=open(clientfile, O_WRONLY|O_CREAT|O_TRUNC, 0666);
if(fdbis<=0)
{
perror("\nopen 2\n");
exit(-1);
}
FILE *fbis=fdopen(fdbis, "wb");
if(fbis==NULL)
{
perror("fdopen");
exit(-1);
}
int WR=fwrite((void*)buf, strlen(buf), 1, fbis);
if(WR<=0)
{
perror("\nfwrite\n");
exit(-1);
}