6

我正在尝试通过 C 中的 TCP 套接字发送图像文件,但图像没有在服务器端正确重组。我想知道是否有人可以指出错误?

我知道服务器正在接收正确的文件大小,它构造了一个该大小的文件,但它不是图像文件。

客户

//Get Picture Size
printf("Getting Picture Size\n");
FILE *picture;
picture = fopen(argv[1], "r");
int size;
fseek(picture, 0, SEEK_END);
size = ftell(picture);

//Send Picture Size
printf("Sending Picture Size\n");
write(sock, &size, sizeof(size));

//Send Picture as Byte Array
printf("Sending Picture as Byte Array\n");
char send_buffer[size];
while(!feof(picture)) {
    fread(send_buffer, 1, sizeof(send_buffer), picture);
    write(sock, send_buffer, sizeof(send_buffer));
    bzero(send_buffer, sizeof(send_buffer));
}

服务器

//Read Picture Size
printf("Reading Picture Size\n");
int size;
read(new_sock, &size, sizeof(int));

//Read Picture Byte Array
printf("Reading Picture Byte Array\n");
char p_array[size];
read(new_sock, p_array, size);

//Convert it Back into Picture
printf("Converting Byte Array to Picture\n");
FILE *image;
image = fopen("c1.png", "w");
fwrite(p_array, 1, sizeof(p_array), image);
fclose(image);

编辑:修复了服务器代码中的 sizeof(int)。

4

3 回答 3

7

您需要在阅读之前找到文件的开头

fseek(picture, 0, SEEK_END);
size = ftell(picture);
fseek(picture, 0, SEEK_SET);

或用于fstat获取文件大小。

于 2012-10-27T05:09:06.890 回答
0

检查freadfwrite语法:

size_t fread(void *ptr, size_t size, size_t n, FILE *fp);

size_t fwrite(const void *ptr, size_t size, size_t n, FILE *fp);

在您的情况下,正确的陈述应该是:

fread(send_buffer, sizeof(send_buffer), 1, picture);

fwrite(p_array, sizeof(p_array), 1,image);

于 2018-03-29T08:17:46.403 回答
0

虽然这是一篇旧文章,但我必须强调原始代码中的一些问题:

  • feof(picture) 在 fopen 之后总是假的。在调用 feof 之前,您必须始终进行阅读
  • read(new_sock, p_array, size) 不保证读取 size 个字节,它取决于 size 的值、网络负载、服务器负载...

一个正确的(至少更健壮的)版本是:

//Send Picture as Byte Array (without need of a buffer as large as the image file)
printf("Sending Picture as Byte Array\n");
char send_buffer[BUFSIZE]; // no link between BUFSIZE and the file size
int nb = fread(send_buffer, 1, sizeof(send_buffer), picture);
while(!feof(picture)) {
    write(sock, send_buffer, nb);
    nb = fread(send_buffer, 1, sizeof(send_buffer), picture);
    // no need to bzero
}

服务器端同样的问题:

//Read Picture Byte Array
printf("Reading Picture Byte Array\n");
char p_array[size];
char* current = p_array;
int nb = read(new_sock, current, size);
while (nb >= 0) {
    current = current + nb;
    nb = read(new_sock, current, size);
}

在服务器端,您可以避免将缓冲区创建为大图像文件(这可能是大图像的问题):

//Read Picture Byte Array and Copy in file
printf("Reading Picture Byte Array\n");
char p_array[BUFSIZE];
FILE *image = fopen("c1.png", "w");
int nb = read(new_sock, p_array, BUFSIZE);
while (nb > 0) {
    fwrite(p_array, 1, nb, image);
    nb = read(new_sock, p_array, BUFSIZE);
}
fclose(image);
于 2018-12-10T17:06:46.790 回答