我正在进行服务器/客户端分配。当我去localhost时,我的服务器应传递给客户端HTML,.txt,.jpg,.gif和.xbm文件以在Web浏览器上打开:8080 /某些地址...我的HTML和.txt文件打开就像一个魅力!但是.jpg和.gif根本不会显示。服务器确实找到了文件,但是当涉及到读取或显示.jpg或.gif时,它无法显示,因为它包含错误,但是当我在常规浏览器中打开它时它运行良好。我们的说明是“当您打开图像文件进行读取时,请确保以二进制模式打开它,因为图像文件包含二进制数据。”所以我这样做但是因为图像没有显示我想知道我是否正确地做了。这是我在c中的代码,用于读取我用于html,txt和图像格式的文件。
void file_read(char *filename){
//filename is actually the path
size_in = strlen(filename);
//this is test display to see how the path was being read and since getting a
//from a web browser contains an extra character '/' in the beginning we need to
//to ignore it,
printf("printf in file_read function %d\n", size_in);
wtg = filename + 1;
printf("printf in file_read function %s\n", wtg);
//file open and transfer in binary 'rb'
fp= fopen (wtg, "rb");
if (fp == NULL){
printf("Could not open the file\n");
exit(0);
}
fseek(fp , 0, SEEK_END);
len = ftell (fp);
rewind (fp);
//allocate memory to contain the whole file:
linehtml = calloc(1, len + 1);
if (linehtml == NULL){fputs("Memory Error", stderr);
exit(2);}
//copy the file into the buffer:
if(1!=fread(linehtml, len, 1, fp)){
fputs("entire read fails",stderr),exit(1);
}
fclose(fp);
/*if (result != len) {
printf("file lenght is %d\n\n", len);
fputs ("Reading Error",stderr);
exit(3);
};*/
}
有什么问题吗?缓冲区不够长吗?任何想法?