输入命令格式:

下载:./SCP_Server 192.168.0.2:/home/pub/file1 ./

上传:./SCP_Client ./file2 192.168.0.2:/home/pub

/*  SCP_Server.c  */

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

#define N 1024

int main(int argc, char *argv[])
{
    struct sockaddr_in servaddr, cliaddr;
    char buf[N], buffer[N]; int lfd, connfd;
    char str[INET_ADDRSTRLEN];
    int n; socklen_t cliaddr_len;

lfd = socket(AF_INET, SOCK_STREAM, 0);

bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(5555);

bind(lfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
    listen(lfd, 20);

printf("Accepting connections ...\n");
    while(1)
    {
        cliaddr_len = sizeof(cliaddr);
        connfd = accept(lfd, (struct sockaddr *)&cliaddr, &cliaddr_len);
        n = read(connfd,buf, N);
///client request get file from server//
        if(buf[0] == 'g')
        {
            n = read(connfd, buf, N);
            printf("Client request get file from server.\n");
            printf("received from %s at PORT %d\n", inet_ntop( AF_INET, &cliaddr.sin_addr, str, sizeof(str)), ntohs(cliaddr.sin_port));
            printf("The file path is:%s\n", buf);
            int fd;
            if((fd = open(buf, O_RDONLY)) < 0)
            {
                perror("open failed");
                write(connfd, "no such file!", 14);
                close(connfd);
                continue;
            }
            n = lseek(fd, 0, SEEK_END);
            sprintf(buf, "%d", n);
            write(connfd, buf, strlen(buf)+1);
            lseek(fd, 0, SEEK_SET);
            read(connfd, buffer, 1);
            while((n = read(fd, buf, N)))
            {
                write(connfd, buf, n);
            }
            close(fd);
            printf("Sending success.\n\n");
        }
///client request put file to server
        else if(buf[0] == 'p')
        {
            buf[0] = '\0';
            n = read(connfd, buf, N);
            printf("Client request put file to server.\n");
            printf("received from %s at PORT %d\n", inet_ntop( AF_INET, &cliaddr.sin_addr, str, sizeof(str)), ntohs(cliaddr.sin_port));
            if(strcmp(buf, "no such file!") == 0)
            {
                printf("wrong filen.\n");
                close(connfd);
                continue;
            }
            printf("The file path is:%s\n", buf);
            int fd;
            if((fd = open(buf, O_RDWR | O_CREAT | O_TRUNC, 0644)) < 0)
            {
                perror("open failed");
                exit(2);
            }
            n = read(connfd, buf, N);
            write(connfd, "", 1);
            int cnt = atoi(buf);
            lseek(fd, cnt - 1, SEEK_SET);
            write(fd, "", 1);
            lseek(fd, 0, SEEK_SET);
            while((n = read(connfd, buf, N)))
            {
                write(fd, buf, n);
            }
            close(fd);
            printf("Getting success.\n\n");
        }
            error                
        else printf("buf error!\n");

close(connfd);
    }

close(lfd);
    return 0;
}

/********* SCP_Client  *********/

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

#define N 1024
#define MAX 256

int main(int argc, char *argv[])
{
    if(argc != 3)
    {
        printf("error command.\n");
        return 0;
    }
    struct sockaddr_in servaddr;
    int sockfd; char buf[N], filename[MAX], buffer[N];
    char *ph, *pc, *ip, *p1, *p2;
    int n, i;
    
    memset(buf,      0, N);
    memset(buffer,   0, N);
    memset(filename, 0, MAX);

p1 = argv[1], p2 = argv[2];
    while(1)
    {
        while(*p1 != ':' && *p1 != '\0') p1++;/* get */
        if(*p1 == ':')
        {
            buf[0] = 'g';
            ip = ph = argv[1];
            pc = argv[2];
            break;
        }
        while(*p2 != ':' && *p2 != '\0') p2++;/* put */
        if(*p2 == ':')
        {
            buf[0] = 'p';
            ip = ph = argv[2];
            pc = argv[1];
            break;
        }
        printf("Wrong command.\n");
        return 1;
    }
    while(*ph != '\0')
    {
        if(*ph++ == ':')
        {
            *(ph - 1) = '\0';
            break;
        }
    }
/get file from server
    if(buf[0] == 'g')
    {
        printf("get file from server.\n");
        for(i = strlen(ph) - 1 ; ; i--)
        {
            if(*(ph + i) == '/')
                break;
        }
        if(*(pc + strlen(pc) - 1) == '/')
            *(pc + strlen(pc) - 1) = '\0';
        sprintf(filename, "%s%s", pc, (ph + i));

sockfd = socket(AF_INET, SOCK_STREAM, 0);
         bzero(&servaddr, sizeof(servaddr));
        servaddr.sin_family = AF_INET;
        if((inet_pton(AF_INET, ip, &servaddr.sin_addr.s_addr)) <= 0)
        {
            perror("wrong ip");
            exit(3);
        }
        servaddr.sin_port = htons(5555);
        if(connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
        {
            perror("can't connect");
            exit(3);
        }
        if(write(sockfd, buf, 1) < 0)
        {
            perror("write g error");
            exit(1);
        }
        if(write(sockfd, ph, strlen(ph) + 1) < 0)
        {
            perror("write filename error");
            exit(1);
        }
        n = read(sockfd, buf, N);
        if(strcmp(buf, "no such file!") == 0)
        {
            printf("No such file, please check the filename.\n");
            exit(1);
        }
        write(sockfd, "", 1);
        int cnt = atoi(buf);
        int fd;
        fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0644 );
        lseek(fd, cnt - 1, SEEK_SET);
        write(fd, "", 1);
        lseek(fd, 0, SEEK_SET);
        while((n = read(sockfd, buf, N)))
        {
            write(fd, buf, n);
        }
        close(fd);
        printf("Getting success.\n");
    }
put file to server
    else if(buf[0] == 'p')
    {
        printf("put file to server.\n");
        for(i = strlen(pc) - 1 ; ; i--)
        {
            if(*(pc + i) == '/')
                break;
        }
        if(*(ph + strlen(ph) - 1) == '/')
            *(ph + strlen(ph) - 1) = '\0';
        sprintf(filename, "%s%s", ph, (pc + i));

sockfd = socket(AF_INET, SOCK_STREAM, 0);
         bzero(&servaddr, sizeof(servaddr));
        servaddr.sin_family = AF_INET;
        if((inet_pton(AF_INET, ip, &servaddr.sin_addr.s_addr)) <= 0)
        {
            perror("wrong ip");
            exit(3);
        }
        servaddr.sin_port = htons(5555);
        if(connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
        {
            perror("can't connect");
            exit(3);
        }
        if(write(sockfd, buf, 1) < 0)
        {
            perror("write p error");
            exit(1);
        }
        int fd;
        if((fd = open(pc, O_RDONLY)) < 0)
        {
            perror("open file failed");
            write(sockfd, "no such file!", 14);
            exit(1);
        }
        if(write(sockfd, filename, strlen(filename) + 1) < 0)
        {
            perror("write filename, error");
            exit(1);
        }
        n = lseek(fd, 0, SEEK_END);
        sprintf(buf, "%d", n);
        write(sockfd, buf, strlen(buf)+1);
        lseek(fd, 0, SEEK_SET);
        read(sockfd, buffer, 1);
        while((n = read(fd, buf, N)))
        {
            write(sockfd, buf, n);
        }
        close(fd);
        printf("Sending success.\n");
    }
///                error                
    else printf("It's wrong.\n");

close(sockfd);

return 0;

}

//  myscp_server.c

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

#define N 1024

int main(int argc, char *argv[])
{
    struct sockaddr_in servaddr, cliaddr;
    char buf[N]; int lfd, connfd;
    char str[INET_ADDRSTRLEN];
    int n; socklen_t cliaddr_len;

lfd = socket(AF_INET, SOCK_STREAM, 0);

bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(5555);

bind(lfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
    listen(lfd, 20);

printf("Accepting connections ...\n");
    while(1)
    {
        cliaddr_len = sizeof(cliaddr);
        connfd = accept(lfd, (struct sockaddr *)&cliaddr, &cliaddr_len);
        n = read(connfd,buf, N);
///client request get file from server//
        if(buf[0] == 'g')
        {
            n = read(connfd, buf, N);
            printf("received from %s at PORT %d\n", inet_ntop( AF_INET, &cliaddr.sin_addr, str, sizeof(str)),
                                ntohs(cliaddr.sin_port));
            printf("The file path is:%s\n\n", buf);
            int fd;
            fd = open(buf, O_RDONLY);
            while((n = read(fd, buf, N)))
            {
                write(connfd, buf, n);
            }
            close(fd);
        }
///client request put file to server
        else if(buf[0] == 'p')
        {
            n = read(connfd, buf, N);
            printf("received from %s at PORT %d\n", inet_ntop( AF_INET, &cliaddr.sin_addr, str, sizeof(str)),
                                ntohs(cliaddr.sin_port));
            printf("The file path is:%s\n\n", buf);
            int fd;
            fd = open(buf, O_RDWR | O_CREAT | O_TRUNC, 0644);
            while((n = read(connfd, buf, N)))
            {
                write(fd, buf, n);
            }
            close(fd);
        }
            error                
        else printf("buf error!\n");

close(connfd);
    }

return 0;
}

/  myscp.c

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

#define N 1024
#define MAX 256

int main(int argc, char *argv[])
{
    if(argc != 3)
    {
        printf("error command.\n");
        return 0;
    }
    struct sockaddr_in servaddr;
    int sockfd; char buf[N], filename[MAX];
    char *ph, *pc, *ip, *p1, *p2;
    int n, i;
    p1 = argv[1], p2 = argv[2];
    while(1)
    {
        while(*p1 != ':' && *p1 != '\0') p1++;/* get */
        if(*p1 == ':')
        {
            buf[0] = 'g';
            ip = ph = argv[1];
            pc = argv[2];
            break;
        }
        while(*p2 != ':' && *p2 != '\0') p2++;/* put */
        if(*p2 == ':')
        {
            buf[0] = 'p';
            ip = ph = argv[2];
            pc = argv[1];
            break;
        }
        printf("Wrong command.\n");
        return 1;
    }
    while(*ph != '\0')
    {
        if(*ph++ == ':')
        {    
            *(ph - 1) = '\0';
            break;
        }
    }
/get file from server
    if(buf[0] == 'g')
    {
        printf("get file from server.\n");
        for(i = strlen(ph) - 1 ; ; i--)
        {
            if(*(ph + i) == '/')
                break;
        }
        if(*(pc + strlen(pc) - 1) == '/')
            *(pc + strlen(pc) - 1) = '\0';
        sprintf(filename, "%s%s", pc, (ph + i));
        printf("ip = %s\nph = %s\npc = %s\nfilename = %s\n", ip, ph, pc, filename);

sockfd = socket(AF_INET, SOCK_STREAM, 0);
        bzero(&servaddr, sizeof(servaddr));
        servaddr.sin_family = AF_INET;
        inet_pton(AF_INET, ip, &servaddr.sin_addr.s_addr);
        servaddr.sin_port = htons(5555);
        if(connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
        {
            perror("wrong ip");
            exit(2);
        }
        if(write(sockfd, buf, 1) < 0)
        {
            perror("write g error");
            exit(1);
        }
        if(write(sockfd, ph, strlen(ph) + 1) < 0)
        {
            perror("write filename error");
            exit(1);
        }
        int fd;
        fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0644 );
        while((n = read(sockfd, buf, N)))
        {
            write(fd, buf, n);
        }
        close(fd);
    }
put file to server
    else if(buf[0] == 'p')
    {
        printf("put file to server.\n");
        for(i = strlen(pc) - 1 ; ; i--)
        {
            if(*(pc + i) == '/')
                break;
        }
        if(*(ph + strlen(ph) - 1) == '/')
            *(ph + strlen(ph) - 1) = '\0';
        sprintf(filename, "%s%s", ph, (pc + i));
        printf("ip = %s\nph = %s\npc = %s\nfilename = %s\n", ip, ph, pc, filename);

sockfd = socket(AF_INET, SOCK_STREAM, 0);
        bzero(&servaddr, sizeof(servaddr));
        servaddr.sin_family = AF_INET;
        inet_pton(AF_INET, ip, &servaddr.sin_addr.s_addr);
        servaddr.sin_port = htons(5555);
        connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
        if(write(sockfd, buf, 1) < 0)
        {
            perror("write p error");
            exit(1);
        }
        if(write(sockfd, filename, strlen(filename) + 1) < 0)
        {
            perror("write filename, error");
            exit(1);
        }
        int fd;
        if((fd = open(pc, O_RDONLY)) < 0)
        {
            perror("open filename failed");
            exit(1);
        }
        while((n = read(fd, buf, N)))
        {
            write(sockfd, buf, n);
        }
        close(fd);
    }
///                error                
    else printf("It's wrong.\n");

close(sockfd);

return 0;
}

bzero
bzero
bzero
bzero
bzero
bzero
bzero
bzero
bzero

类SCP程序(无身份认证)相关推荐

  1. 支付宝身份认证(刷脸)小程序PHP

    支付宝小程序的身份认证,需要添加支付宝(支付宝身份验证)能力,签约成功后可以开始了 TP代码 public function aliUserCode(): array {// 引入支付宝SDKvend ...

  2. ASP.NET MVC身份认证与授权

    文章目录 一.引言 1.身份认证的含义 2.身份认证与Session 基于Session保存用户状态 不足之处 二.ASP.NET身份验证 Forms验证 FormsAuthentication类 验 ...

  3. ASP.NET MVC --- 身份认证与授权

    身份认证的含义:例如在现在浏览的某宝电商网站中会经常遇到的情况,想要将某件商品加入到购物车,点击"加入购物车"之后弹出的却是登录界面.这个就是所谓的请求的身份认证 身份认证与Ses ...

  4. typeorm 修改事务_nest.js + typeORM: 身份认证, 事务管理

    nest.js + typeORM: 身份认证, 事务管理 知识点 jwt 身份认证 md5 加密 typeorm 事务 (transaction) 的使用 本文会延续上一篇文章 https://ww ...

  5. 【Web安全】一款功能强大的Web身份认证测试框架

    关于Raider Raider是一款功能强大的Web身份认证测试框架,该框架被设计用来测试Web应用程序的身份认证机制.虽然像ZAProxy和Burpsuite这样的Web代理工具同样可以允许研究人员 ...

  6. 小程序 身份认证服务器,如何实现微信小程序与.net core应用服务端的无状态身份验证...

    随着.net core2的发布,越来越多人使用.net core2开发各种应用服务端,下面我就结合自己最近开发的一款小程序,给大家分享下,怎么使用小程序登录后,小程序与服务端交互的权限控制. 服务端的 ...

  7. 构建具有用户身份认证的 React + Flux 应用程序

    序言:这是一篇内容详实的 React + Flux 教程,文章主要介绍了如何使用 API 获取远程数据以及如何使用 JSON Web Tokens 进行用户身份认证.在阅读本文之后,我一直使用文章介绍 ...

  8. 曾经的习武少年,如今的锦佰安CEO:他立志要开启身份认证的无密时代

    [数据猿导读] 未来的身份认证,不再需要密码. 记者 | 小北 官网 | www.datayuan.cn 微信公众号ID | datayuancn   壹 眼前的风宁,与记者想象中不太一样. 少林寺习 ...

  9. 生物识别最新进展:动态密码语音无监督身份认证系统通过科技成果鉴定

    近日,由中国电子学会主持召开的"基于动态密码语音的无监督身份认证系统"科技成果鉴定会在清华大学举办,AI科技大本营受邀出席. 该成果由清华大学.北京得意音通技术有限责任公司共同完成 ...

最新文章

  1. OpenCV 笔记 -- 边缘检测(Sobel、Laplace、Canny)
  2. dnslog在mysql在linux_DNSLog在MySQL注入中的实战
  3. 清华计算机学院吴建平,吴建平
  4. windchill 可交付成果 文档_敏捷等于没有文档吗?敏捷项目管理VS传统项目管理区别在哪里?...
  5. 举例讲清楚模型树和回归树的区别
  6. glTF格式初步了解
  7. 使用.Net6中的System.Text.Json遇到几个常见问题及解决方案
  8. python5个功能_5个常用的定制Python功能代码
  9. [运动][组合]睡前运动
  10. 神经网络与深度学习——TensorFlow2.0实战(笔记)(四)(python文件)
  11. 停止过度设计,开发客户需要的软件
  12. python互斥锁原理_Linux 互斥锁的实现原理(pthread_mutex_t)
  13. oracle用户被锁
  14. 专访京东副总裁翁志:全方位解读 CNCC 2018「数据开创商业新生态」技术论坛 | CNCC 2018...
  15. 2013年云计算发展展望:混合云即将起飞
  16. 2021:An Improved Attention for Visual Question Answering
  17. 在线流程图和思维导图开发技术详解(五)
  18. 通过经纬度坐标计算距离
  19. 华三交换机配置access命令_h3c交换机配置命令
  20. LKDHelper使用LKDBHelper以实体类对象进行数据库的操作,例如新建一个新闻实体类,以这个类来

热门文章

  1. Automation 360 新版AA
  2. 分享一个可以wget下载的java8(jdk8/jre8)包
  3. Photoshop 实例教程
  4. 【Java】一文搞定Java反射技术
  5. 【监控仪表系统】Grafana 中文入门教程 | 构建你的第一个仪表盘
  6. 关于solidworksPDM插件的制作
  7. 视频教程-大牛带你全面剖析Python高频面试真题-Python
  8. Java实现斗地主升级版
  9. 海信智能电视可以升级鸿蒙系统吗,都是55寸新品,荣耀智慧屏、小米电视和海信电视,你会咋选?...
  10. 信息系统监理与审计 我国实践与美国的经验