发送文件从客户端到服务器使用套接字在c on linux

1sbrub3j  于 2023-02-15  发布在  Linux
关注(0)|答案(1)|浏览(117)

我对C语言和Linux都很陌生,英语不是我的母语。提前向你道歉。我需要做的是用Linux上的socket将.avi.mp4文件从客户端发送到服务器。我可以将文件从客户端发送到服务器,但服务器上的视频无法工作。
当我尝试播放视频时,我不断收到类似"无法确定流类型"的错误。当我检查客户端上的原始视频时,大小为5,787,969字节,但服务器上传输的视频大小为5,786,954字节。我认为这是因为传输文件时数据丢失。
我该怎么解决这个问题?
这是我的代码如下:

服务器

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>     // read, write
#include <arpa/inet.h>
#include <sys/types.h>  // socket, bind, accept, open
#include <sys/socket.h> // socket, bind, listen, accept
#include <sys/stat.h>   // open
#include <fcntl.h>      // open
#include <errno.h>

#define PORT    5500
#define MAXBUF  1024

int main() {
    int server_sockfd;
    int client_sockfd;
    int des_fd; // file num
    struct sockaddr_in serveraddr, clientaddr;
    int client_len, read_len, file_read_len;    // length
    char buf[MAXBUF];

    int check_bind;
    client_len = sizeof(clientaddr);

    /* socket() */
    server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if(server_sockfd == -1) {
        perror("socket error : ");
        exit(0);
    }

    /* bind() */
    bzero(&serveraddr, sizeof(serveraddr));
    serveraddr.sin_family       = AF_INET;
    serveraddr.sin_addr.s_addr  = htonl(INADDR_ANY);
    serveraddr.sin_port         = htons(PORT);

    if(bind(server_sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) > 0) {
        perror("bind error : ");
        exit(0);
    }

    /* listen */
    if(listen(server_sockfd, 5) != 0) {
        perror("listen error : ");
    }

    while(1) {
        char file_name[MAXBUF]; // local val
        memset(buf, 0x00, MAXBUF);

        /* accept() */
        client_sockfd = accept(server_sockfd, (struct sockaddr *)&clientaddr, &client_len);
        printf("New Client Connect : %s\n", inet_ntoa(clientaddr.sin_addr));

        /* file name */
        read_len = read(client_sockfd, buf, MAXBUF);
        if(read_len > 0) {
            strcpy(file_name, buf);
            printf("%s > %s\n", inet_ntoa(clientaddr.sin_addr), file_name);
        } else {
            close(client_sockfd);
            break;
        }

        /* create file */

        des_fd = open(file_name, O_WRONLY | O_CREAT | O_EXCL, 0700);
        if(!des_fd) {
            perror("file open error : ");
            break;
        }   
        /* file save */
        while(1) {
            memset(buf, 0x00, MAXBUF);
            file_read_len = read(client_sockfd, buf, MAXBUF);
            write(des_fd, buf, file_read_len);
            if(file_read_len == EOF | file_read_len == 0) {
                printf("finish file\n");
                break;
            }

        }

        close(client_sockfd);
        close(des_fd);
    }
    close(server_sockfd);
    return 0;
}

客户

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

#define PORT    5500
#define IP      "127.0.0.1"
#define MAXBUF  1024

int main() {
    struct sockaddr_in serv_addr;
    int     s;
    int         sourse_fd;
    char        buf[MAXBUF];
    int         file_name_len, read_len;
    /* socket() */
    s = socket(AF_INET, SOCK_STREAM, 0);
    if(s == -1) {
        return 1;
    }
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = inet_addr(IP);
    serv_addr.sin_port = htons(PORT);

    if(connect(s, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) == -1) {
        perror("connect : ");
        printf("fail to connect.\n");
        close(s);
        return 1;
    }

    memset(buf, 0x00, MAXBUF);
    printf("write file name to send to the server:  ");
    scanf("%s", buf);

    printf(" > %s\n", buf);
    file_name_len = strlen(buf);

    send(s, buf, file_name_len, 0);
    sourse_fd = open(buf, O_RDONLY);
    if(!sourse_fd) {
        perror("Error : ");
        return 1;
    }

    while(1) {
        memset(buf, 0x00, MAXBUF);
        read_len = read(sourse_fd, buf, MAXBUF);
        send(s, buf, read_len, 0);
        if(read_len == 0) {
            break;
        }

    }

    return 0;
}
kmpatx3s

kmpatx3s1#

在服务器中

查看Server中的“文件保存”while循环:

/* file save */
while(1) {
    memset(buf, 0x00, MAXBUF);
    file_read_len = read(client_sockfd, buf, MAXBUF);
    write(des_fd, buf, file_read_len);
    if(file_read_len == EOF | file_read_len == 0) {
        printf("finish file\n");
        break;
    }
}

您应该只使用file_read_len == 0而不是file_read_len == EOF | file_read_len == 0
仍然在Server中,在创建文件的 open() 函数中,我还建议将0700更改为S_IRWXU & (~S_IXUSR)__,但它不是强制性的。有关详细信息,请参见user@host:~$ man 2 open

在客户端中

Client中,您只需在 strlen(buf) 中添加“+ 1”:
file_name_len = strlen(buf) + 1;
这是因为字符串需要以空字节结尾,而 strlen() 返回字符串长度减去结尾的空字节:-)
我还建议您使用 argcargv 来获取参数,请看:

#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

int
main (int argc, char **argv) {
    int how_many_files = argc - 1; /* '- 1' cause first arg is program name
                                    */
    int fd; /* file descriptor */
    int i; /* a counter */
    char buffer[10]; /* a buffer */

    for (i = 0; i < how_many_files; i++) {
        fd = open(argv[i + 1], O_RDONLY);
        if (fd == -1)
            continue;

        read(fd, buffer, 10);
        buffer[9] = '\0'; /* last byte of buffer needs to be null char*/
        printf("First 10 Bytes of the file is: %s\n",
               buffer);

        close(fd);
    }

    return 0;
}

相关问题