[國嵌攻略][178][網絡安全傳輸系統框架搭建]

客戶端代碼編寫安全

1.創建鏈接服務器

    1.1.建立socket網絡

    1.2.初始化地址socket

1.3.鏈接服務器函數

2.實現上傳和下載,實現菜單ui

    2.1.上傳文件spa

        2.1.1.發送操做類型碼code

        2.1.2.打開文件server

2.1.3.發送文件名blog

        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:\n\t./client <ip address>\n");
        return -1;
    }
    
    //建立標識
    int clientfd;
    
    clientfd = socket(AF_INET, SOCK_STREAM, 0);
    if(clientfd == -1){
        printf("Can not create socket!\n");
        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!\n");
        return -1;
    }

    //顯示菜單
    menu(clientfd);
    
    //關閉鏈接
    close(clientfd);
    
    return 0;
}

/********************************************************************
*名稱:menu
*參數:
*    clientfd   客戶端標誌
*返回:
*    none
*功能:顯示菜單
*********************************************************************/
void menu(int clientfd){
    //顯示菜單
    while(1){
        //打印菜單
        printf("\n");
        printf("**********************************************************************\n");
        printf("*                               Client                               *\n");
        printf("*[1]Upload Files                                                     *\n");
        printf("*[2]Download Files                                                   *\n");
        printf("*[3]Exit                                                             *\n");
        printf("**********************************************************************\n");
        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("\nPlease input again!\n");
                break;
        }
        
        //是否退出
        if(num == 3){
            break;
        }
    }
}

/********************************************************************
*名稱:upload
*參數:
*    clientfd   客戶端標誌
*返回:
*    none
*功能:上傳文件
*********************************************************************/
void upload(int clientfd){
    //輸入文件名稱
    char filename[20];
    
    printf("\nUpload file: ");
    scanf("%s", &filename);
    
    //打開上傳文件
    int fd;
    
    fd = open(filename, O_RDONLY);
    if(fd == -1){
        printf("Can not open file!\n");
        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!\n");
        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("\nDownload file: ");
    scanf("%s", &filename);
    
    //建立下載文件
    int fd;
    
    fd = open(filename, O_RDWR | O_CREAT, 0777);
    if(fd == -1){
        printf("Can not create file!\n");
        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!\n");
        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!\n");
        return -1;
    }
    
    //監聽端口
    int isListen;
    
    isListen = listen(serverfd, 5);
    if(isListen == -1){
        printf("Can not listen!\n");
        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!\n");
            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!\n");
        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!\n");
        return ;
    }
    
    //發送文件長度
    struct stat fstat;
    int isState;
    
    isState = stat(filename, &fstat);
    if(isState == -1){
        printf("Can not get file state!\n");
        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);
}
相關文章
相關標籤/搜索