程序卡住了将内容写入C中的文件 [英] Program stuck writing contents to the file in C

查看:66
本文介绍了程序卡住了将内容写入C中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将文件从客户端发送到服务器.

I'm sending a file from the client to the server.

  • 客户端发送文件名
  • 服务器接收文件名
  • 客户端发送文件大小
  • 服务器接收到文件大小
  • 客户端发送文件内容
  • 服务器接收文件内容

当我打印出我发送的文件大小时,它是我希望发送的文件的确切字节数,这样就可以了.问题在于,写入新文件时,服务器(接收)似乎没有退出while循环.我知道这是因为最终的打印语句printf(服务器已收到请求的文档\ n");永远不会到达,它只是挂起.可能是什么原因造成的?

When I print out the file size that I sent, it is the exact number of bytes as the file I wish to send so that's fine. The issue is that the server (receives) doesn't seem to exit the while loop when writing to the new file. I know this because the final print statement printf("The server has received the requested document\n"); is never reached and it just hangs. What could be causing this?

客户端代码段(发送):

Client snippet (sends):

 else if(strcmp(shortCommand, "put") == 0){
            
            
            char *tmp = buf + 4;
            char filename[MAX_BLOCK_SIZE];
        size_t size, bytes_read, bytes_written;
        int x;

            strcpy(filename, "filename ");
            strcat(filename, tmp);
            FILE *fp;
            printf("File name: %s\n", tmp);
            fp = fopen(tmp, "rb");
            if(fp == NULL){
                
                printf("ERROR: Requested file does not exist.\n");
                
            }
            else{
            printf("Client sending filename...\n");
            if ((x = write(sd, buf, sizeof(buf))) < 0){     //sending the file name to the client first
                printf("Error sending client's filename.\n");
            }

            printf("Client sending file...\n");
            
            fseek(fp, 0, SEEK_END);
            size = ftell(fp);
            fseek(fp, 0, SEEK_SET);
            printf("Sending file size\n");
            
            if((write(sd, &size, sizeof(size))) < 0){ //sending filesize
                printf("error sending file size\n");
            }
            
            printf("Sending file\n");
            while((bytes_read = fread(buf, 1, sizeof(buf), fp)) > 0){ //sending file contents
            
                if ((bytes_written = write(sd, buf, bytes_read)) < 0){
                    printf("Error sending client file.\n");
                }
            
            }
            printf("bytes written: %ld\n", bytes_written);
            fclose(fp);
            }   
    }

服务器代码段(接收):

Server snippet (receives):

if(strcmp(shortCommand, "put") == 0){
                char *tmp = buf + 4;
                char filename2[MAX_BLOCK_SIZE];
                size_t  filesize;
                size_t total_bytes_read = 0;
                ssize_t bytes_read = 0;
                size_t error;
                FILE *fp;
                strcpy(filename2, tmp);
                printf("Server receiving file name...\n"); //filename is received on the first read before this IF
                fp = fopen(filename2, "wb");
                if(fp == NULL){
                    printf("File could not be opened.\n");
                    exit(1);
                }
                
                printf("Server receiving file size...\n");
                
                if((error = read(sd, &filesize, sizeof(filesize))) < 0){ //receiving file size
                    perror("Error reading filesize\n");
                    exit(1);
                }
                
                printf("Filesize is: %ld \n", filesize);
                
                while(total_bytes_read < filesize){
                    while((bytes_read = read(sd, buf, sizeof(buf))) > 0){ //receving file contents and writing to file
                        fwrite(buf, 1, bytes_read, fp);
                        total_bytes_read += bytes_read;
                    if(ferror(fp)){
                        perror("error");
                        fclose(fp);
                    }
                    }
                }
                printf("The server has received the requested document.\n");
                fflush(stdout);
                fclose(fp);
    }

强制退出程序后,实际上可以看到文件已被复制.只是不要退出while循环,让我回到客户端.

After I exit the program by force, I can actually see that the file has been copied. Just doesn't exit that while loop to let me go back to the client.

推荐答案

进行一些基本调试的时间.我建议将您的读取循环更改为以下内容:

Time for some basic debugging. I'd suggest changing your read loop to something like this:

while (total_bytes_read < filesize) {
    printf("DEBUG A: total=%zu, size=%zu\n", total_bytes_read, filesize);
    while ((bytes_read = read(sd, buf, sizeof(buf))) > 0) {
        printf("DEBUG B: read=%zd\n", bytes_read);
        fwrite(buf, 1, bytes_read, fp);
        total_bytes_read += bytes_read;
        printf("DEBUG C: total=%zu\n", total_bytes_read);
        if (ferror(fp))
            printf("DEBUG D\n");
            perror("error");
            fclose(fp);
        }
        printf("DEBUG E\n");
    }
    printf("DEBUG F\n");
}
printf("DEBUG G\n");

然后运行它,通过less或其他一些传呼器将输出管道化,然后希望它应该可以更清楚地了解实际发生的情况.

Then run it, piping the output through less or some other pager, it should then hopefully become clearer what's actually happening.

可以随意发布此修改后的代码的输出(在评论中或在实际问题中),我们无疑将能够帮助进行分析.

Feel free to post the output of this modified code (in a comment, or in the actual question), we'll no doubt be able to help with the analysis.

这篇关于程序卡住了将内容写入C中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆