• Linux项目:文件传输


    文件传输:在两台计算机之间进行文件的传递

    客户端与服务器连接,客户端与服务器端不在同一个主机之上,要求客户端可以查看服务器端有哪些文件,删除文件,移动文件,归类文件,增添文件夹;下载文件;上传文件,断点续传。
    几乎所有的服务器端程序都会有配置文件

    自定义文件
    (1)socket_info.conf

      1 #
      2 #
      3 
      4 #ip地址
      5 ips=127.0.0.1
      6 
      7 #端口
      8 port=6000
      9 
     10 #监听队列大小
     11 lismax=5
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    (2)ser.c

    #include "sock_init.h"
    #include "work_thread.h"
    
    int main()
    {
        int sockfd = socket_init();
        if ( sockfd == -1 )
        {
            printf("ser create sockfd failed\n");
            exit(0);
        }
    
        while( 1 )
        {
            struct sockaddr_in caddr;
            int len = sizeof(caddr);
    
            int c = accept(sockfd,(struct sockaddr*)&caddr,&len);
            if ( c < 0 )
            {
                continue;
            }
    
            printf("accept c=%d\n",c);
    
            thread_start(c);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    (3)makefile

    all: ser
    
    ser : ser.o sock_init.o work_thread.o  
    	gcc -o ser ser.o sock_init.o work_thread.o -lpthread
    
    ser.o : ser.c 
    	gcc -c ser.c 
    
    sock_init.o : sock_init.c
    	gcc -c sock_init.c 
    
    work_thread.o: work_thread.c 
    	gcc -c work_thread.c
    
    clean:
    	rm -f *.o ser
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    (4)sock_init.h

    #ifndef  __SOCK_INIT
    #define  __SOCK_INIT
    
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    int socket_init();
    
    #endif
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    (5)sock_init.c

    #include "sock_init.h"
    
    #define  IPS    "ips="
    #define  PORT   "port="
    #define  LISMAX "lismax="
    
    struct sock_info
    {
        char ips[32];
        short port;
        int lismax;
    };
    
    int read_sock_conf(struct sock_info * dt)
    {
        if ( dt == NULL )
        {
            return -1;
        }
    
        FILE * fp = fopen("socket_info.conf","r");
        if ( fp == NULL )
        {
            printf("open socket_info.conf failed\n");
            return -1;
        }
    
        int index = 0;
        while( 1 )
        {
            char buff[128] = {0};
            char * s = fgets(buff,128,fp);
            if ( s == NULL )
            {
                break;
            }
    
            index++;
    
            if ( s[0] == '#' || s[0] == '\n' )
            {
                continue;
            }
    
            s[strlen(s)-1] = '\0';
    
            if ( strncmp(s,IPS,strlen(IPS)) == 0 )
            {
                strcpy(dt->ips,s+strlen(IPS));
            }
            else if ( strncmp(s,PORT,strlen(PORT)) == 0 )
            {
                dt->port = atoi(s+strlen(PORT));
            }
            else if ( strncmp(s,LISMAX,strlen(LISMAX)) == 0 )
            {
                dt->lismax=atoi(s+strlen(LISMAX));
            }
            else
            {
                printf("不识别的参数:line %d\n",index);
            }
    
    
        }
    
        fclose(fp);
        return 0;
    }
    
    int socket_init()
    {
        struct sock_info data;
        if ( read_sock_conf(&data) == -1 )
        {
            return -1;
        }
    
        printf("ip=%s\n",data.ips);
        printf("port=%d\n",data.port);
        printf("lismax=%d\n",data.lismax);
    
        int sockfd = socket(AF_INET,SOCK_STREAM,0);
        if ( sockfd == -1 )
        {
            return -1;
        }
    
        struct sockaddr_in saddr;
        memset(&saddr,0,sizeof(saddr));
        saddr.sin_family = AF_INET;
        saddr.sin_port = htons(data.port);
        saddr.sin_addr.s_addr = inet_addr(data.ips);
    
        int res = bind(sockfd,(struct sockaddr*)&saddr,sizeof(saddr));
        if ( res == -1 )
        {
            return -1;
        }
    
        if ( listen(sockfd,data.lismax) == -1 )
        {
            return -1;
        }
        
        return sockfd;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108

    (6)work_thread.h

    #ifndef __THREAD_START
    #define __THREAD_START
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    void thread_start(int c);
    
    #endif
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    (7)work_thread.c

    #include "work_thread.h"
    
    void* work_thread(void* arg)
    {
    
    }
    
    void thread_start(int c)
    {
        pthread_t id;
        pthread_create(&id,NULL,work_thread,(void*)c);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
  • 相关阅读:
    如何有效的禁止Google Chrome自动更新?
    手机兼容性测试
    职业技术认证:《研发效能(DevOps)工程师》——开启职业发展新篇章
    腾讯云优惠券种类、领取方法及使用教程分享
    day007--MySQL中的数值函数
    元宇宙基础理论、架构设计、关键技术和行业应用-总纲
    第二章 学生指导(04 小学德育 05 小学美育 06 小学生安全与心理健康教育 07 学校与家庭、社会的协调)
    易排通用规划平台,以Excel作为数据源的调用方法与数据文件说明
    OpenAI GPT-4.5 Turbo 泄露,六月或将发布
    百战RHCE(第四十一战:linux高级应用-重置root密码)
  • 原文地址:https://blog.csdn.net/qq_48580892/article/details/121192171