[国嵌攻略][178][网络安全传输系统框架搭建]

客户端代码编写

1.建立连接

    1.1.创建socket

    1.2.初始化地址

1.3.连接服务器

2.实现上传和下载,实现菜单

    2.1.上传文件

        2.1.1.发送操作类型码

        2.1.2.打开文件

2.1.3.发送文件名

        2.1.4.发送文件长度

        2.1.5.发送文件内容

    2.2.下载文件

        2.2.1.发送操作类型码

        2.2.2.发送文件名

        2.2.3.创建文件

        2.2.4.接收文件长度

        2.2.5.接收文件内容

    2.3.退出程序

        2.3.1.发送退出操作码

        2.3.2.清屏

3.关闭连接

服务器代码编写

1.建立客户端连接

    1.1.创建socket

    1.2.绑定地址

    1.3.监听

    1.4.等待连接

2.响应客户端请求

    2.1.读取命令码

    2.2.处理退出请求

        2.2.1.退出循环

    2.3.处理上传请求

        2.3.1.接收文件名

        2.3.2.创建文件

        2.3.3.接收文件长度

        2.3.4.接收文件内容

    2.4.处理下载请求

        2.4.1.接收文件名

        2.4.2.找到并打开文件

        2.4.3.发送文件长度

    2.4.4.发送文件内容

client.c

/********************************************************************
*名称:client.c
*作者:D
*时间:2016.04.03
*功能:网络安全传输系统客户端
*********************************************************************/

/********************************************************************
*头文件
*********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>

/********************************************************************
*宏定义
*********************************************************************/
#define SERVER_PORT 3333   //网络端口

/********************************************************************
*函数原型
*********************************************************************/
int main(int argc, char **argv);
void menu(int clientfd);
void upload(int clientfd);
void download(int clientfd);
void quit(int clientfd);

/********************************************************************
*名称:main
*参数:
*    argc   参数数量
*    argv   参数列表
*返回:
*    stat    0 成功
*           -1 失败
*功能:主函数
*********************************************************************/
int main(int argc, char **argv){
    //参数检查
    if(argc != 2){
        printf("Usage:
	./client <ip address>
");
        return -1;
    }
    
    //创建标识
    int clientfd;
    
    clientfd = socket(AF_INET, SOCK_STREAM, 0);
    if(clientfd == -1){
        printf("Can not create socket!
");
        return -1;
    }
    
    //建立连接
    struct sockaddr_in serverAddr;
    int isConnect;
    
    serverAddr.sin_family = AF_INET;                   //设置协议
    serverAddr.sin_port = htons(SERVER_PORT);          //设置端口
    serverAddr.sin_addr.s_addr = inet_addr(argv[1]);   //设置地址
    bzero(serverAddr.sin_zero, 8);                     //设置为零
    
    isConnect = connect(clientfd, (struct sockaddr *)&serverAddr, sizeof(struct sockaddr));
    if(isConnect == -1){
        printf("Can not connect to server!
");
        return -1;
    }

    //显示菜单
    menu(clientfd);
    
    //关闭连接
    close(clientfd);
    
    return 0;
}

/********************************************************************
*名称:menu
*参数:
*    clientfd   客户端标志
*返回:
*    none
*功能:显示菜单
*********************************************************************/
void menu(int clientfd){
    //显示菜单
    while(1){
        //打印菜单
        printf("
");
        printf("**********************************************************************
");
        printf("*                               Client                               *
");
        printf("*[1]Upload Files                                                     *
");
        printf("*[2]Download Files                                                   *
");
        printf("*[3]Exit                                                             *
");
        printf("**********************************************************************
");
        printf("Please Select: ");
        
        //输入命令
        int num;
        
        scanf("%d", &num);
        
        //处理命令
        switch(num){
            //上传文件
            case 1:
                upload(clientfd);
                break;
            
            //下载文件
            case 2:
                download(clientfd);
                break;
            
            //退出程序
            case 3:
                quit(clientfd);
                break;
                
            //错误命令
            default:
                printf("
Please input again!
");
                break;
        }
        
        //是否退出
        if(num == 3){
            break;
        }
    }
}

/********************************************************************
*名称:upload
*参数:
*    clientfd   客户端标志
*返回:
*    none
*功能:上传文件
*********************************************************************/
void upload(int clientfd){
    //输入文件名称
    char filename[20];
    
    printf("
Upload file: ");
    scanf("%s", &filename);
    
    //打开上传文件
    int fd;
    
    fd = open(filename, O_RDONLY);
    if(fd == -1){
        printf("Can not open file!
");
        return ;
    }
    
    //发送上传命令
    char cmd = 'U';
    
    write(clientfd, (void *)&cmd, sizeof(cmd));
    
    //发送文件名称
    int namesize;
    
    namesize = strlen(filename) + 1;   //加上字符串接收符
    
    write(clientfd, (void *)&namesize, sizeof(namesize));
    write(clientfd, (void *)&filename, namesize);
    
    //发送文件长度
    struct stat fstat;
    int isState;
    
    isState = stat(filename, &fstat);
    if(isState == -1){
        printf("Can not get file state!
");
        return ;
    }
    
    write(clientfd, (void *)&(fstat.st_size), sizeof(fstat.st_size));
    
    //发送文件内容
    char buf[1024];
    int  num;
    
    num = read(fd, (void *)buf, sizeof(buf));
    while(num > 0){
        //发送文件内容
        write(clientfd, (void *)&buf, num);
        
        //读取文件内容
        num = read(fd, (void *)buf, sizeof(buf));
    }
    
    //关闭上传文件
    close(fd);
}

/********************************************************************
*名称:download
*参数:
*    clientfd   客户端标志
*返回:
*    none
*功能:下载文件
*********************************************************************/
void download(int clientfd){
    //输入文件名称
    char filename[20];
    
    printf("
Download file: ");
    scanf("%s", &filename);
    
    //创建下载文件
    int fd;
    
    fd = open(filename, O_RDWR | O_CREAT, 0777);
    if(fd == -1){
        printf("Can not create file!
");
        return ;
    }
    
    //发送下载命令
    char cmd = 'D';
    
    write(clientfd, (void *)&cmd, sizeof(cmd));
    
    //发送文件名称
    int namesize;
    
    namesize = strlen(filename) + 1;   //加上字符串接收符
    
    write(clientfd, (void *)&namesize, sizeof(namesize));
    write(clientfd, (void *)&filename, namesize);
    
    //接收文件长度
    int fileszie;
    
    read(clientfd, &fileszie, sizeof(fileszie));
    
    //接收文件内容
    char buf[1024];
    int  num;
    
    num = read(clientfd, (void *)buf, sizeof(buf));
    while(num > 0){
        //写入接收内容
        write(fd, (void *)&buf, num);
        
        //是否接收结束
        fileszie = fileszie - num;
        if(fileszie == 0){
            break;
        }
        
        //接收文件内容
        num = read(clientfd, (void *)buf, sizeof(buf));
    }
    
    //关闭下载文件
    close(fd);
}

/********************************************************************
*名称:quit
*参数:
*    clientfd   客户端标志
*返回:
*    none
*功能:退出程序
*********************************************************************/
void quit(int clientfd){
    //发送退出命令
    char cmd = 'Q';
    
    write(clientfd, (void *)&cmd, sizeof(cmd));
    
    //清除屏幕显示
    system("clear");
}

server.c

/********************************************************************
*名称:server.c
*作者:D
*时间:2016.04.05
*功能:网络安全传输系统服务端
*********************************************************************/

/********************************************************************
*头文件
*********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>

/********************************************************************
*宏定义
*********************************************************************/
#define SERVER_PORT 3333  //网络端口

/********************************************************************
*函数原型
*********************************************************************/
int main(int argc, char **argv);
void menu(int clientfd);
void upload(int clientfd);
void download(int clientfd);

/********************************************************************
*名称:main
*参数:
*    argc   参数数量
*    argv   参数列表
*返回:
*    stat    0 成功
*           -1 失败
*功能:主函数
*********************************************************************/
int main(int argc, char **argv){
    //创建标识
    int serverfd;
    
    serverfd = socket(AF_INET, SOCK_STREAM, 0);
    if(serverfd == -1){
        printf("Can not create socket!
");
        return -1;
    }
    
    //绑定地址
    struct sockaddr_in serverAddr;
    int isBand;
    
    serverAddr.sin_family = AF_INET;                   //设置协议
    serverAddr.sin_port = htons(SERVER_PORT);          //设置端口
    serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);    //设置地址
    bzero(serverAddr.sin_zero, 8);                     //设置为零
    
    isBand = bind(serverfd, (struct sockaddr *)&serverAddr, sizeof(struct sockaddr));
    if(isBand == -1){
        printf("Can not bind!
");
        return -1;
    }
    
    //监听端口
    int isListen;
    
    isListen = listen(serverfd, 5);
    if(isListen == -1){
        printf("Can not listen!
");
        return -1;
    }
    
    //处理连接
    while(1){
        //等待连接
        socklen_t clientAddrLen;
        struct sockaddr_in clientAddr;
        int clientfd;

        clientAddrLen = sizeof(struct sockaddr);
        clientfd = accept(serverfd, (struct sockaddr *)&clientAddr, &clientAddrLen);
        if(clientfd == -1){
            printf("Can not accept!
");
            return -1;
        }
        
        //处理菜单
        menu(clientfd);
        
        //关闭连接
        close(clientfd);
    }
    
    //关闭连接
    close(serverfd);
}

/********************************************************************
*名称:menu
*参数:
*    clientfd   客户端标志
*返回:
*    none
*功能:处理菜单
*********************************************************************/
void menu(int clientfd){
    //处理菜单
    while(1){
        //读取命令
        char cmd;
    
        read(clientfd, (void *)&cmd, sizeof(cmd));
        
        //处理命令
        switch(cmd){
            //上传文件
            case 'U':
                upload(clientfd);
                break;
            
            //下载文件
            case 'D':
                download(clientfd);
                break;
                
            //退出程序
            case 'Q':
                break;
                
            //其他命令
            default:
                break;
        }
        
        //是否退出
        if(cmd == 'Q'){
            break;
        }
    }
}

/********************************************************************
*名称:upload
*参数:
*    clientfd   客户端标志
*返回:
*    none
*功能:上传文件
*********************************************************************/
void upload(int clientfd){
    //接收文件名称
    int namesize;
    char filename[20];
    
    read(clientfd, (void *)&namesize, sizeof(namesize));
    read(clientfd, (void *)&filename, namesize);
    
    //创建上传文件
    int fd;
    
    fd = open(filename, O_RDWR | O_CREAT, 0777);
    if(fd == -1){
        printf("Can not create file!
");
        return ;
    }
    
    //接收文件长度
    int fileszie;
    
    read(clientfd, &fileszie, sizeof(fileszie));
    
    //接收文件内容
    char buf[1024];
    int  num;
    
    num = read(clientfd, (void *)buf, sizeof(buf));
    while(num > 0){
        //写入接收内容
        write(fd, (void *)&buf, num);
        
        //是否接收结束
        fileszie = fileszie - num;
        if(fileszie == 0){
            break;
        }
        
        //接收文件内容
        num = read(clientfd, (void *)buf, sizeof(buf));
    }
    
    //关闭上传文件
    close(fd);
}

/********************************************************************
*名称:download
*参数:
*    clientfd   客户端标志
*返回:
*    none
*功能:下载文件
*********************************************************************/
void download(int clientfd){
    //接收文件名称
    int namesize;
    char filename[20];
    
    read(clientfd, (void *)&namesize, sizeof(namesize));
    read(clientfd, (void *)&filename, namesize);
    
    //打开下载文件
    int fd;
    
    fd = open(filename, O_RDONLY);
    if(fd == -1){
        printf("Can not open file!
");
        return ;
    }
    
    //发送文件长度
    struct stat fstat;
    int isState;
    
    isState = stat(filename, &fstat);
    if(isState == -1){
        printf("Can not get file state!
");
        return ;
    }
    
    write(clientfd, (void *)&(fstat.st_size), sizeof(fstat.st_size));
    
    //发送文件内容
    char buf[1024];
    int  num;
    
    num = read(fd, (void *)buf, sizeof(buf));
    while(num > 0){
        //发送文件内容
        write(clientfd, (void *)&buf, num);
        
        //读取文件内容
        num = read(fd, (void *)buf, sizeof(buf));
    }
    
    //关闭下载文件
    close(fd);
}
原文地址:https://www.cnblogs.com/d442130165/p/5355060.html