12

我正在尝试在服务器和客户端之间进行文件传输,但工作非常糟糕。基本上需要发生的是:
1)客户端向服务器发送一个txt文件(我称之为“quotidiani.txt”)
2)服务器将其保存在另一个txt文件中(“receive.txt”)
3)服务器运行上面的脚本修改它并用另一个名称(“output.txt”)保存它
4)服务器将文件发送回客户端,用名称(final.txt)保存它(在同一个套接字上)

问题是第一个文件 (quotidiani.txt) 只读取了一小部分,然后出现了一些错误。我希望有人帮助我理解和纠正我的错误。

这是我的代码:

客户端.c:

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <signal.h>
#include <ctype.h>          
#include <arpa/inet.h>
#include <netdb.h>

#define PORT 20000
#define LENGTH 512 


void error(const char *msg)
{
    perror(msg);
    exit(1);
}

int main(int argc, char *argv[])
{
    /* Variable Definition */
    int sockfd; 
    int nsockfd;
    char revbuf[LENGTH]; 
    struct sockaddr_in remote_addr;

    /* Get the Socket file descriptor */
    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    {
        fprintf(stderr, "ERROR: Failed to obtain Socket Descriptor! (errno = %d)\n",errno);
        exit(1);
    }

    /* Fill the socket address struct */
    remote_addr.sin_family = AF_INET; 
    remote_addr.sin_port = htons(PORT); 
    inet_pton(AF_INET, "127.0.0.1", &remote_addr.sin_addr); 
    bzero(&(remote_addr.sin_zero), 8);

    /* Try to connect the remote */
    if (connect(sockfd, (struct sockaddr *)&remote_addr, sizeof(struct sockaddr)) == -1)
    {
        fprintf(stderr, "ERROR: Failed to connect to the host! (errno = %d)\n",errno);
        exit(1);
    }
    else 
        printf("[Client] Connected to server at port %d...ok!\n", PORT);

    /* Send File to Server */
    //if(!fork())
    //{
        char* fs_name = "/home/aryan/Desktop/quotidiani.txt";
        char sdbuf[LENGTH]; 
        printf("[Client] Sending %s to the Server... ", fs_name);
        FILE *fs = fopen(fs_name, "r");
        if(fs == NULL)
        {
            printf("ERROR: File %s not found.\n", fs_name);
            exit(1);
        }

        bzero(sdbuf, LENGTH); 
        int fs_block_sz; 
        while((fs_block_sz = fread(sdbuf, sizeof(char), LENGTH, fs)) > 0)
        {
            if(send(sockfd, sdbuf, fs_block_sz, 0) < 0)
            {
                fprintf(stderr, "ERROR: Failed to send file %s. (errno = %d)\n", fs_name, errno);
                break;
            }
            bzero(sdbuf, LENGTH);
        }
        printf("Ok File %s from Client was Sent!\n", fs_name);
    //}

    /* Receive File from Server */
    printf("[Client] Receiveing file from Server and saving it as final.txt...");
    char* fr_name = "/home/aryan/Desktop/progetto/final.txt";
    FILE *fr = fopen(fr_name, "a");
    if(fr == NULL)
        printf("File %s Cannot be opened.\n", fr_name);
    else
    {
        bzero(revbuf, LENGTH); 
        int fr_block_sz = 0;
        while((fr_block_sz = recv(sockfd, revbuf, LENGTH, 0)) > 0)
        {
            int write_sz = fwrite(revbuf, sizeof(char), fr_block_sz, fr);
            if(write_sz < fr_block_sz)
            {
                error("File write failed.\n");
            }
            bzero(revbuf, LENGTH);
            if (fr_block_sz == 0 || fr_block_sz != 512) 
            {
                break;
            }
        }
        if(fr_block_sz < 0)
        {
            if (errno == EAGAIN)
            {
                printf("recv() timed out.\n");
            }
            else
            {
                fprintf(stderr, "recv() failed due to errno = %d\n", errno);
            }
        }
        printf("Ok received from server!\n");
        fclose(fr);
    }
    close (sockfd);
    printf("[Client] Connection lost.\n");
    return (0);
}

服务器.c

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <signal.h>
#include <ctype.h>          
#include <arpa/inet.h>
#include <netdb.h>

#define PORT 20000 
#define BACKLOG 5
#define LENGTH 512 

void error(const char *msg)
{
    perror(msg);
    exit(1);
}

int main ()
{
    /* Defining Variables */
    int sockfd; 
    int nsockfd; 
    int num;
    int sin_size; 
    struct sockaddr_in addr_local; /* client addr */
    struct sockaddr_in addr_remote; /* server addr */
    char revbuf[LENGTH]; // Receiver buffer

    /* Get the Socket file descriptor */
    if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1 )
    {
        fprintf(stderr, "ERROR: Failed to obtain Socket Descriptor. (errno = %d)\n", errno);
        exit(1);
    }
    else 
        printf("[Server] Obtaining socket descriptor successfully.\n");

    /* Fill the client socket address struct */
    addr_local.sin_family = AF_INET; // Protocol Family
    addr_local.sin_port = htons(PORT); // Port number
    addr_local.sin_addr.s_addr = INADDR_ANY; // AutoFill local address
    bzero(&(addr_local.sin_zero), 8); // Flush the rest of struct

    /* Bind a special Port */
    if( bind(sockfd, (struct sockaddr*)&addr_local, sizeof(struct sockaddr)) == -1 )
    {
        fprintf(stderr, "ERROR: Failed to bind Port. (errno = %d)\n", errno);
        exit(1);
    }
    else 
        printf("[Server] Binded tcp port %d in addr 127.0.0.1 sucessfully.\n",PORT);

    /* Listen remote connect/calling */
    if(listen(sockfd,BACKLOG) == -1)
    {
        fprintf(stderr, "ERROR: Failed to listen Port. (errno = %d)\n", errno);
        exit(1);
    }
    else
        printf ("[Server] Listening the port %d successfully.\n", PORT);

    int success = 0;
    while(success == 0)
    {
        sin_size = sizeof(struct sockaddr_in);

        /* Wait a connection, and obtain a new socket file despriptor for single connection */
        if ((nsockfd = accept(sockfd, (struct sockaddr *)&addr_remote, &sin_size)) == -1) 
        {
            fprintf(stderr, "ERROR: Obtaining new Socket Despcritor. (errno = %d)\n", errno);
            exit(1);
        }
        else 
            printf("[Server] Server has got connected from %s.\n", inet_ntoa(addr_remote.sin_addr));

        /*Receive File from Client */
        char* fr_name = "/home/aryan/Desktop/receive.txt";
        FILE *fr = fopen(fr_name, "a");
        if(fr == NULL)
            printf("File %s Cannot be opened file on server.\n", fr_name);
        else
        {
            bzero(revbuf, LENGTH); 
            int fr_block_sz = 0;
            while((fr_block_sz = recv(nsockfd, revbuf, LENGTH, 0)) > 0) 
            {
                int write_sz = fwrite(revbuf, sizeof(char), fr_block_sz, fr);
                if(write_sz < fr_block_sz)
                {
                    error("File write failed on server.\n");
                }
                bzero(revbuf, LENGTH);
                if (fr_block_sz == 0 || fr_block_sz != 512) 
                {
                    break;
                }
            }
            if(fr_block_sz < 0)
            {
                if (errno == EAGAIN)
                {
                    printf("recv() timed out.\n");
                }
                else
                {
                    fprintf(stderr, "recv() failed due to errno = %d\n", errno);
                    exit(1);
                }
            }
            printf("Ok received from client!\n");
            fclose(fr); 
        }

        /* Call the Script */
        system("cd ; chmod +x script.sh ; ./script.sh");

        /* Send File to Client */
        //if(!fork())
        //{
            char* fs_name = "/home/aryan/Desktop/output.txt";
            char sdbuf[LENGTH]; // Send buffer
            printf("[Server] Sending %s to the Client...", fs_name);
            FILE *fs = fopen(fs_name, "r");
            if(fs == NULL)
            {
                fprintf(stderr, "ERROR: File %s not found on server. (errno = %d)\n", fs_name, errno);
                exit(1);
            }

            bzero(sdbuf, LENGTH); 
            int fs_block_sz; 
            while((fs_block_sz = fread(sdbuf, sizeof(char), LENGTH, fs))>0)
            {
                if(send(nsockfd, sdbuf, fs_block_sz, 0) < 0)
                {
                    fprintf(stderr, "ERROR: Failed to send file %s. (errno = %d)\n", fs_name, errno);
                    exit(1);
                }
                bzero(sdbuf, LENGTH);
            }
            printf("Ok sent to client!\n");
            success = 1;
            close(nsockfd);
            printf("[Server] Connection with Client closed. Server will wait now...\n");
            while(waitpid(-1, NULL, WNOHANG) > 0);
        //}
    }
}
4

2 回答 2

8

一些评论没有特别的顺序:

  • 您经常错过了解确切错误的机会:

    if(listen(sockfd,BACKLOG) == -1)
    {
        printf("ERROR: Failed to listen Port %d.\n", PORT);
        return (0);
    }
    

    这个块绝对应该包括一个perror("listen")或类似的东西。当perror()错误strerror()详细信息将通过errno. 有确切的失败原因将在编程时为您节省时间,并且在将来事情无法按预期工作时为您和您的用户节省时间。

  • 您的错误处理需要进一步标准化:

    if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1 )
    {
        printf("ERROR: Failed to obtain Socket Descriptor.\n");
        return (0);
    }
    

    这不应该 return 0因为这将向 shell 发出信号,表明程序运行完成而没有错误。您应该return 1(或使用EXIT_SUCCESSand EXIT_FAILURE)表示异常退出。

     else 
        printf("[Server] Server has got connected from %s.\n", inet_ntoa(addr_remote.sin_addr));
    
     /*Receive File from Client */
    

    在前面的块中,您遇到了错误情况,但仍继续执行。这是获得非常不良行为的快速方法。这应该重新启动主服务器循环或退出子进程或类似的东西。(取决于您是否保留多进程服务器。)

    if(!fork())
    {
    

    前面的块忘记了fork() 失败的原因。fork()可以而且确实会失败——尤其是在大学常见的共享托管环境中——所以你应该准备好完整的、复杂的三个可能的返回值fork():失败、孩子、父母。

  • 看来您是在fork()乱用;您的客户端和服务器都非常简单,它们的设计运行方式意味着它们不能用于同时为多个客户端提供服务。您可能应该为每个进程都坚持一个进程,至少在算法被完美调试并且您找到同时运行多个客户端的方法之前。我希望这是您现在遇到的问题的根源。

  • 你需要使用函数来封装细节;编写一个连接到服务器的函数、一个发送文件的函数、一个写入文件的函数等。编写一个函数来处理复杂的部分写入。(我特别建议从Unix 环境中的高级编程一书的源代码中窃取writen函数。文件。)如果您正确编写函数,您可以在客户端和服务器中重复使用它们。(就像将它们放入并编译程序一样。)lib/writen.cutils.cgcc -o server server.c utils.c

    拥有每个只做一件事的较小功能将使您可以一次专注于较少量的代码,为每个代码编写少量测试,这将帮助您缩小哪些代码部分仍需要改进。

于 2012-06-29T00:38:35.200 回答
6

这里似乎缺少一个讨论点,所以我想在这里提一下。

让我们快速了解 TCP 的数据传输。有三个步骤 a) 连接建立,b) 数据传输,c) 连接终止

现在,客户端通过 TCP 套接字向服务器发送文件。

服务器对文件进行一些处理并将其发送回客户端。

现在需要完成所有 3 个步骤。连接建立是通过调用 connect 来完成的。数据读取/写入由此处的接收/发送完成,连接终止由关闭完成。

这里的服务器使用recv循环读取数据。现在循环什么时候结束?当 recv 返回 0 或可能小于 0 时出错。 什么时候recv返回0?-> 当对方关闭连接时。 (当 TCP FIN 段已被本方接收时)。

所以在这段代码中,当客户端完成发送文件时,我使用了一个关闭函数,它从客户端发送 FIN 段,服务器的 recv 现在可以返回 0 并且程序继续。(中途关闭,因为客户端后续还需要读取数据)。

(只是为了理解,请注意 TCP 的连接建立是 3 次握手,连接终止是 4 次握手。)

同样,如果您忘记关闭服务器端的连接套接字,客户端的 recv 也将永远阻塞。我认为这就是您提到的有时使用 ctrl c 停止客户端的原因。

你可以请。请参阅任何标准网络书籍或 rfc http://www.ietf.org/rfc/rfc793.txt了解更多关于 TCP

我已经粘贴了修改后的代码,也很少添加一些评论,

希望这个解释会有所帮助。

修改客户端代码:

 while((fs_block_sz = fread(sdbuf, sizeof(char), LENGTH, fs)) > 0)
    {
        if(send(sockfd, sdbuf, fs_block_sz, 0) < 0)
        {
            fprintf(stderr, "ERROR: Failed to send file %s. (errno = %d)\n", fs_name, errno);
            exit(1);
        }
        bzero(sdbuf, LENGTH);
    }

 /*Now we have sent the File's data, what about server's recv?
 Recv is blocked  and waiting for data to arrive or if the protocol
 stack receives a TCP FIN segment ..then the recv will return 0 and
 the server code can continue */
 /*Sending the TCP FIN segment by shutdown and this is half way
 close, since the client also needs to read data subsequently*/

 shutdown(sockfd, SHUT_WR);
 printf("Ok File %s from Client was Sent!\n", fs_name);
于 2012-07-03T08:21:42.367 回答