1

I am running into problems when I try to transfer a file between the simple server and client applications that I have written. The file gets transferred successfully, but the file size is different at the receiving side (server side). I open the file on the client side, use fseek() to find the size of the file. Then I use fread() to to read it into a buffer of char type. I send this buffer using sendto() as I have to use UDP sockets. On the server side, I use recvfrom() to store this and use fwrite() to write it into another file. But when I check the size of the file, it is much bigger. Also I am not able to open it even though it is supposed to be a text file. Can you give me some pointers as to where I might be going wrong? Also is this the best way to send files over sockets? Are there better methods to send files?

Thanks

Code for client side

//Writing code to open file and copy it into buffer
        fseek(fp, 0, SEEK_END);
        size_t file_size = ftell(fp);
        fseek(fp, 0, SEEK_SET);
        if(fread(file_buffer, file_size, 1, fp)<=0)
        {
            printf("Unable to copy file into buffer! \n");
            exit(1);
        }

        //Sending file buffer
        if(sendto(sock, file_buffer, strlen(file_buffer), 0, (struct sockaddr *) &serv_addr, serv_len)<0)
        {
            printf("Error sending the file! \n");
            exit(1);
        }
        bzero(file_buffer, sizeof(file_buffer));

Code on the server side to receive the file

//Receiving file from client
        char file_buffer[BUFSIZE];
        if(recvfrom(sock, file_buffer, BUFSIZE, 0, (struct sockaddr *) &client_addr, &client_addr_size)<0)
        {
            printf("Error receiving file.");
            exit(1);
        }

        char new_file_name[] = "copied_";
        strcat(new_file_name,file_name);
        FILE *fp;
        fp = fopen(new_file_name,"w+");
        if(fwrite(file_buffer, 1, sizeof(file_buffer), fp)<0)
        {
            printf("Error writing file! \n");
            exit(1);
        }
4

1 回答 1

1

您的代码有几个问题。

在发件人中:

  • 你如何分配file_buffer?如果文件大于缓冲区怎么办?(可能这不会导致手头的问题。)

  • 您只需检查返回值fread是否为<= 0. 实际上,如果发生错误或 EOF,则返回值可以是小于文件完整大小的任何值。(可能这不会导致手头的问题。)

  • 您传递strlen(file_buffer)而不是传递file_sizesendto系统调用。strlen查找文件可能包含或不包含的 NUL 字节。它可能不包含任何内容,因为您说它是一个文本文件。

    • 如果文件包含至少一个 NUL 字节,则数据包将在第一个 NUL 字节之前被截断,您将不会传输文件的全部内容。
    • 如果文件不包含 NUL 字节,strlen则将扫描超出文件末尾作为读入缓冲区。您的程序会因为扫描到未分配的内存地址而崩溃strlen,或者您将在文件末尾发送额外的垃圾。

在接收器中:

  • 您忽略了返回值,recvfrom它是接收到的数据包的有效负载长度。之后,您将无法知道您收到了多少数据。

  • 当你fwrite得到结果时,你传递sizeof(file_buffer)的是大小而不是接收到的实际数据量。这是一个固定值 ( BUFSIZE),可能比您的文件大。写入磁盘的文件将包含超出文件末尾的垃圾。

于 2012-10-01T13:52:20.903 回答