• FTP 1.0


    自己写的可以实现文件的下载(必须自己知道文件名),还有很多要优化。

    譬如:不能看可以下载的文件,输入错误无法处理,不能处理多个用户,每次只能下载一个结束,服务器没有完成守护进程;没有用函数封装,简化main程序。

    高级功能:支持文件夹选择下载,断点续传,尝试用多线程,多进程,线程池处理多个请求。

     1 客户端代码:
     2 头文件
     3 #ifndef __CLIENT_COMMON__
     4 #define __CLIENT_COMMON__
     5 
     6 #include<stdio.h>
     7 #include<stdlib.h>
     8 #include<string.h>
     9 
    10 #include<sys/types.h>
    11 #include<sys/socket.h>
    12 
    13 #include<netinet/in.h>
    14 #include<arpa/inet.h>
    15 
    16 #include<unistd.h>
    17 #include<fcntl.h>
    18 
    19 #define SIZE 1024
    20 #define DOWN_PATH "/home/luolizhi/Downloads"
    21 
    22 
    23 #endif
    24 
    25 
    26 client main
    27 
    28 #include"client_common.h"
    29 
    30 int main(int argc, char* argv[])    //serverip port
    31 {
    32     if(argc != 3)
    33     {
    34         printf("EXE IP PORT");
    35         exit(1);
    36     }
    37 
    38     //socket
    39     int clientfd = socket(AF_INET,SOCK_STREAM,0);
    40     if(clientfd == -1)
    41     {
    42         perror("clientfd");
    43         exit(1);
    44     }
    45     
    46     struct sockaddr_in serveraddr;
    47     serveraddr.sin_family = AF_INET;
    48     serveraddr.sin_addr.s_addr = inet_addr(argv[1]);
    49     serveraddr.sin_port = htons(atoi(argv[2]));
    50 
    51     if(-1 == connect(clientfd,(struct sockaddr*)&serveraddr,sizeof(struct sockaddr)))
    52     {
    53         perror("connect");
    54         exit(1);
    55     }
    56     
    57     //send
    58     char send_buf[SIZE]="";
    59     fgets(send_buf,sizeof(send_buf),stdin);
    60     send(clientfd,send_buf,strlen(send_buf),0);
    61 
    62     //recv
    63     /*
    64     char FULL_PATH[SIZE]="";
    65     sprintf(FULL_PATH,"%s/%s",DOWN_PATH,send_buf);
    66     FULL_PATH[strlen(FULL_PATH)-1]='';
    67 */
    68     send_buf[strlen(send_buf)-1]='';
    69     FILE* fp = fopen(send_buf,"w");
    70 
    71     char recv_buf[SIZE] = "";
    72     recv(clientfd,recv_buf,sizeof(recv_buf)-1,0);
    73     //printf("recv:%s
    ",recv_buf);
    74     fwrite(recv_buf,1,strlen(recv_buf),fp);
    75     
    76     fclose(fp);
    77 
    78 
    79     close(clientfd);
    80 
    81 
    82 
    83 
    84     return 0;
    85 }

    高级功能:实现文件夹选择下载,断点续传。(目前实现由困难)

      1 服务器端代码:
      2 
      3 头文件
      4 #ifndef __SERVER_COMMON__
      5 #define __SERVER_COMMON__
      6 
      7 #include<stdio.h>
      8 #include<stdlib.h>
      9 #include<string.h>
     10                         //socket
     11 #include<sys/types.h>
     12 #include<sys/socket.h>
     13                         //inet
     14 #include<netinet/in.h>
     15 #include<arpa/inet.h>
     16                         //FILE
     17 #include<sys/types.h>
     18 #include<sys/stat.h>
     19 #include<fcntl.h>
     20 
     21 #define DOWN_PATH "/home/luolizhi/Downloads"
     22 #define SIZE 1024
     23 
     24 
     25 void recvn(int sfd,int len);
     26 void sendn(int sfd,int len);
     27 
     28 #endif
     29 
     30 
     31 main 程序
     32 #include"server_common.h"
     33 
     34 int main(int argc, char* argv[])    //ip port
     35 {
     36     if(argc != 3)
     37     {
     38         printf("EXE IP PORT
    ");
     39         exit(1);
     40     }
     41     //socket
     42     int serverfd = socket(AF_INET,SOCK_STREAM,0);
     43     if(serverfd == -1)
     44     {
     45         perror("socket");
     46         exit(1);
     47     }
     48 
     49     //bind
     50     struct sockaddr_in serveraddr;
     51     serveraddr.sin_family = AF_INET;
     52     serveraddr.sin_addr.s_addr = inet_addr(argv[1]);
     53     serveraddr.sin_port = htons(atoi(argv[2]));
     54 
     55     if(-1 == bind(serverfd,(struct sockaddr*)&serveraddr,sizeof(struct sockaddr)))
     56     {
     57         perror("bind");
     58         exit(1);
     59     }
     60 
     61     //listen
     62     if(-1 == listen(serverfd,10))
     63     {
     64         perror("listen");
     65         exit(1);
     66     }
     67     
     68     //accept
     69     int clientfd;
     70     struct sockaddr_in clientaddr;
     71     socklen_t clientlen = sizeof(clientaddr);
     72 
     73     clientfd = accept(serverfd,(struct sockaddr*)&clientaddr,&clientlen);
     74     if(clientfd == -1)
     75     {
     76         perror("accept");
     77         exit(1);
     78     }
     79 
     80     printf("client: ip:%s,port:%d on
    ",inet_ntoa(clientaddr.sin_addr),ntohs(clientaddr.sin_port));
     81 
     82     //recv
     83     char recv_buf[SIZE] = "";
     84     char FULL_PATH[SIZE] = "";
     85     
     86     recv(clientfd,recv_buf,sizeof(recv_buf)-1,0);
     87     
     88     sprintf(FULL_PATH,"%s/%s",DOWN_PATH,recv_buf);
     89     FULL_PATH[strlen(FULL_PATH)-1] = '';
     90     printf("download file:%s
    ",FULL_PATH);
     91 
     92     //open file
     93     FILE* fp = fopen(FULL_PATH,"r");
     94     if(fp == NULL)
     95     {
     96         perror("fopen");
     97         exit(1);
     98     }
     99     
    100     //read file
    101     char send_buf[SIZE] = "";
    102     size_t readn;
    103     readn = fread(send_buf,1,sizeof(send_buf),fp);
    104     printf("readn:%d
    ",readn);
    105 
    106     //send
    107     size_t sendn;
    108     sendn = send(clientfd,send_buf,strlen(send_buf),0);
    109     if(sendn == -1)
    110     {
    111         perror("sendn");
    112         exit(1);
    113     }
    114 
    115     printf("sendn:%d
    ",sendn);
    116 
    117     close(clientfd);
    118     close(serverfd);
    119 
    120     //while(1);
    121     return 0;
    122 }
  • 相关阅读:
    Cloud7为全球移动互联网大会提供手机门户支撑 开源CMS
    揭东县信息中心采用We7站群管理系统重新架构揭东县政务网 开源CMS
    中国计算机报:We7云计算重塑政府门户网站群 开源CMS
    We7网站群系统全新打造辽宁省民政厅网站群平台 开源CMS
    We7网站群为华中科技大学同济医学院附属同济医院提供专业网... 开源CMS
    sso实现原理解析
    TDSQL | DB·洞见回顾|基于LSMTree存储的数据库性能改进
    又拿奖了!腾讯云原生数据库TDSQLC斩获2021PostgreSQL中国最佳数据库产品奖
    为云而生,云原生数据库TDSQLC技术突破与演进
    金融级数据库新坐标:腾讯云TDSQL发布全自研新敏态引擎
  • 原文地址:https://www.cnblogs.com/luolizhi/p/4554274.html
Copyright © 2020-2023  润新知