【问题标题】:Resume file upload/download after lost connection (Socket programming) using c program [duplicate]使用c程序在丢失连接(套接字编程)后恢复文件上传/下载[重复]
【发布时间】:2020-10-06 20:58:23
【问题描述】:

我需要在 C 程序中使用 tcp/ip 套接字连接丢失后恢复从客户端到服务器的上传文件我不知道该怎么做。(不希望重新发送最初发送的数据)我会如果有人能给我建议我应该如何在套接字服务器客户端中实现文件恢复功能,真的很感激?

【问题讨论】:

  • 只是普通的 TCP 套接字?然后你需要在 TCP 之上实现一个应用层协议,它应该有你需要的特性。
  • 这里的重要教训:在发布问题之前始终先搜索。在您撰写此问题时,@BradSchoening 链接的问题很可能已列在您的“相关”列中。始终先检查“相关”问题,看看他们是否回答了您的问题。

标签: c sockets tcp network-programming client-server


【解决方案1】:

在 TCP 或 UDP 中没有内置这样的机制。您需要让您的服务器和客户端就特定条款达成一致。如果你认为没有这样的协议,并且你想从头开始,那么放弃这个想法。您可以选择HTTP,它提供对Partial-Content 的支持。出于学习目的,您绝对可以尝试自己编写一个。下面我实现了一个这样的最小协议:

规则:

1) 客户端以网络字节顺序发送编码为 32 位 unsigned int 的文件偏移量(在下载开始时,发送 0)。

2) 服务器读取偏移量并转换为主机字节顺序并从该特定偏移量发送数据。

client.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>

int main()
{
    /* Initialize the file */

    int fd = open("download", O_RDWR | O_CREAT);

    struct stat statbuf;
    fstat(fd, &statbuf);

    uint32_t cli_fsize = statbuf.st_size;

    /* Initialize the connection with the server */

    int sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    struct sockaddr_in servaddr;
    memset(&servaddr, 0, sizeof(servaddr));

    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
    servaddr.sin_port = htons(55555);

    if (connect(sockfd, (struct sockaddr*) &servaddr, sizeof(servaddr)) != 0) {
        exit(1);
    }

    /* Write the file offset to the server */

    uint32_t n_cli_fsize = htonl(cli_fsize);

    int off_write = 0;

    for ( ; ; ) {
        int wr_return = write(sockfd, (void*) &n_cli_fsize + off_write, sizeof(n_cli_fsize) - off_write);

        if (wr_return <= 0)
            exit(1);

        off_write = off_write + wr_return;

        if (off_write == sizeof(n_cli_fsize))
            break;
    }

    /* Read the data from the socket and write to the file */

    char fbuf[100];

    for ( ; ; ) {
        int rd_return = read(sockfd, fbuf, sizeof(fbuf));

        if (rd_return < 0)
            exit(1);
        else if (rd_return == 0)
            break;

        int wr_status = 0;

        for ( ; ; ) {
            int wr_return = pwrite(fd, fbuf + wr_status, rd_return - wr_status, cli_fsize);

            if (wr_return < 0)
                exit(1);

            wr_status = wr_status + wr_return;
            cli_fsize = cli_fsize + wr_return;

            if (wr_status == rd_return)
                break;
        }
    }

    return 0;
}

服务器.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main()
{
    /* File initializations */

    FILE* fp = fopen("data.bin", "r+");

    if (fp == NULL)
        exit(1);

    struct stat statbuf;

    if (fstat(fileno(fp), &statbuf) != 0)
        exit(0);

    uint32_t fsize = statbuf.st_size;

    /* Initialize the server */

    int sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP), clifd, addr_len;

    struct sockaddr_in servaddr, cliaddr;
    memset(&servaddr, 0, sizeof(servaddr));

    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(55555);

    if (bind(sockfd, (struct sockaddr*) &servaddr, sizeof(servaddr)) != 0) {
        exit(1);
    }

    listen(sockfd, 5);

    for ( ; ; ) {

        addr_len = sizeof(cliaddr);

        if ((clifd = accept(sockfd, (struct sockaddr*) &cliaddr, &addr_len)) < 0) {
            goto next_cli;
        }

        /* Read the 32 bit offset */

        uint32_t n_cli_fsize = 0;
        int off_read = 0;

        for ( ; ; ) {
            int rd_status = read(clifd, (void*) &n_cli_fsize, sizeof(n_cli_fsize) - off_read);

            if (rd_status == -1)
                goto next_cli;
            else if (rd_status == 0 && off_read != sizeof(n_cli_fsize))
                goto next_cli;

            off_read = off_read + rd_status;

            if (off_read == sizeof(n_cli_fsize))
                break;
        }

        uint32_t cli_fsize = ntohl(n_cli_fsize);

        /* Read from the file and write to the socket */

        char fbuf[100];

        for ( ; ; ) {
            int rd_return = pread(fileno(fp), fbuf, sizeof(fbuf), cli_fsize);

            if (rd_return <= 0)
                goto next_cli;

            cli_fsize = cli_fsize + rd_return;

            int wr_status = 0;

            for ( ; ; ) {
                int wr_return = write(clifd, fbuf + wr_status, rd_return - wr_status);

                if (wr_return <= 0)
                    goto next_cli;

                wr_status = wr_status + wr_return;

                if (wr_status == rd_return)
                    break;
            }
        }

        next_cli:

        if (clifd >= 0)
            close(clifd);
    }

    return 0;
}

数据.bin

$ du -h data.bin && md5sum data.bin 
149M    data.bin
6f188c0f60376fed5cfa00f55681b436  data.bin

注意:服务器始终在所有终端会话中运行。

Terminal Session 1(允许客户端完全下载文件):

$ du -h download 
du: cannot access 'download': No such file or directory
$ ./client 
$ du -h download && md5sum download
149M    download
6f188c0f60376fed5cfa00f55681b436  download

终端会话 2(客户端在下载过程中中断并恢复):

$ du -h download 
du: cannot access 'download': No such file or directory
$ ./client
^C
$ du -h download && md5sum download
80M     download
0f77e56c7a512abc4ccafdb890f451a1  download
$ ./client
$ du -h download && md5sum download
149M    download
6f188c0f60376fed5cfa00f55681b436  download

终端会话 3(在完全获取的文件上运行客户端):

$ du -h download  && md5sum download 
149M    download
6f188c0f60376fed5cfa00f55681b436  download
$ ./client
$ du -h download  && md5sum download 
149M    download
6f188c0f60376fed5cfa00f55681b436  download

您可以围绕这个基本实现进行调整,并添加更多功能。

【讨论】:

  • 谢谢...@Mohith Reddy 我曾尝试使用 NCFTPPUT 功能将文件发送到服务器端口,现在它工作正常,现在我需要暂停并使用 NCFTP 恢复传输。该过程是当传输文件时不存在互联网时,它应该暂停状态,当互联网到来时,文件传输应该恢复(不希望重新发送最初发送的数据)NCFTP中有任何cmds???跨度>
  • @Surya 我个人没用过ncftpput,但是从man page看来,好像支持上传恢复(-z)。你试过吗?
猜你喜欢
  • 2014-04-02
  • 2014-08-10
  • 1970-01-01
  • 1970-01-01
  • 2020-12-25
  • 1970-01-01
  • 1970-01-01
  • 2017-01-16
  • 1970-01-01
相关资源
最近更新 更多