linux 实现ftp多客户端连接访问

时间:2022-10-09 19:27:36

在复习阶段,我又重新写了一个ftp服务程序。实现了多客户端同时连接、操作。


文件目录:
common.h
ftpserver.c
ftpclient.c

common.h
定义数据结构,以及常用函数

#ifndef COMMON_H
#define COMMON_H

#define SIZE_REQ 88

#define SIZE_RESP_DATA 1000
#define SIZE_RESP_INFO 36
#define SIZE_RESP 1036

#define PATH "./"

typedef struct RequestBody{
char type;//4
char s_arg[40];
char s_arg2[40];
int i_arg;//4
}RequestBody;

typedef struct ResponeBody{
char type;//2 byte
char status;//2 byte
int datalen;//4 byte
int datatotal;//4 byte
char arg[24];//24 byte
char data[SIZE_RESP_DATA];
}ResponeBody;

typedef enum RequestType{
none, list, get, put,
login, regist, say, quit, clear, getData, putData
}RequestType;

char *RequestTypeKey[] = {"none", "list", "get", "put",
"login", "regist", "say", "quit", "clear", "getData", "putData"};

typedef struct ThreadArg{
int i_arg;
int i_arg2;
char s_arg[50];
char s_arg2[50];
}ThreadArg;

int my_write(int socket, void *data, int len){
if(socket == 0 || data == NULL || len <= 0){
return -1;
}
int i = 0;
int len_t = 0;
while(i < len){
len_t = len-i > SIZE_RESP ? SIZE_RESP : len-i;
i += write(socket, data+i, len_t);
}
return len;
}

void my_read(int socket, void *data, int len){
int i = 0;
int len_t = 0;
while(i < len){
len_t = len-i > SIZE_RESP ? SIZE_RESP : len-i;
i += read(socket, data+i, len_t);
}
}

#endif // COMMON_H

ftpserver.c
ftp服务器程序

/*************************************************************************
> File Name: telser.c
# File Name: telser.c
# Author :pengjing
# QQ : 784915651
# Email:784915651@qq.com
************************************************************************/


#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <pthread.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
//IO multiplexing
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
//select
#include <sys/select.h>
#include <sys/time.h>
#include <unistd.h>
//socket config
#include <sys/socket.h>
//sqlite3
#include <sqlite3.h>
//helper interface
#include "common.h"
#include "seqlist.h"

//socket init
int my_socket_listen(const char *ip, int port);
int my_socket_while_select(int listenfd);

//business handle
int business_rwaync_config();

//helper
RequestType get_request_type(const char *type, char *status);

//business handle
int handle_op(int confd_t);
int handle_list(int socket);
void* handle_get(void *arg);
void* handle_put(void *arg);
int handle_login(int socket, const char *username, const char *userpwd);
int handle_regist(int socket, const char *username, const char *userpwd);
int handle_say(int socket, const char *data);

//define macro
#define DB_PATH "../ftpdb.db"

//define global var
fd_set set_rfd;//create file desc set
sqlite3 *db;//database
Seqlist *op_files = NULL;
pthread_mutex_t op_files_mutex;

int main(int argc, char *argv[])
{
//user input ip,port
/*
if(argc != 3){
printf("./tels <ip> <port>");
return -1;
}*/

//open database
int res = sqlite3_open(DB_PATH, &db);
if(SQLITE_OK != res){
printf("%s\n", sqlite3_errmsg(db));
return -1;
}

//user config
res = business_rwaync_config();
if(res < 0){
return -1;
}
//test
//int ttt = handle_login("pengjing", "123456");
/*int ttt = handle_regist(7, "test2", "test2");
if(ttt == 0){
printf("login success\n");
}else{
printf("login fail\n");
}
return 0;*/


//socket listen
int listenfd = my_socket_listen(NULL, 11223);
if(listenfd < 0){
return -1;
}

my_socket_while_select(listenfd);

sqlite3_close(db); //close database
return 0;
}

int my_socket_listen(const char *ip, int port){
int listenfd = socket(AF_INET, SOCK_STREAM, 0);
if(-1 == listenfd)
{
perror("server socket error");
return -1;
}

int optval = 1;
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));

struct sockaddr_in addr;
addr.sin_family = AF_INET;
//addr.sin_port = htons(atoi(argv[2]));
//addr.sin_addr.s_addr = inet_addr(argv[1]);
addr.sin_port = htons(port);
if(ip == NULL){
addr.sin_addr.s_addr = INADDR_ANY;
}else{
addr.sin_addr.s_addr = inet_addr(ip);
}

int res = bind(listenfd, (struct sockaddr *)&addr, sizeof(addr));
if(-1 == res)
{
perror("server bind error");
return -1;
}

res = listen(listenfd, 100);
if(-1 == res)
{
perror("server listen error");
return -1;
}
printf("server init success\n");
return listenfd;
}

int my_socket_while_select(int listenfd){
//check client connect request
struct sockaddr_in addr_con;
int size_addr_con = sizeof(addr_con);
memset(&addr_con, 0, size_addr_con);
int confd = -1;
int res = -1;
fd_set set_rfd_t;
FD_ZERO(&set_rfd);
FD_SET(listenfd, &set_rfd);//add a listenfd to the collection
int max_rfd = listenfd;//max file desc + 1
struct timeval timeout;
int fd = 0;
while(1){
//looping data reassign
set_rfd_t = set_rfd;
timeout.tv_sec = 2;
timeout.tv_usec = 0;

//loop check is file desc need op
res = select(max_rfd+1, &set_rfd_t, NULL, NULL, &timeout);
if(res < 0){
perror("select error");
break;
}else if(0 == res){
printf("timeout !\n");
continue;
}
//need op
int count = max_rfd;
for(fd = 0; fd <= count; fd++){
if(FD_ISSET(fd, &set_rfd_t) > 0){
if(fd == listenfd){
//is listend socket
confd = accept(listenfd, (struct sockaddr *)&addr_con, &size_addr_con);
if(-1 == confd)
{
perror("client connect fail");
continue;
}
printf("IP %s connect\n", inet_ntoa(addr_con.sin_addr));
FD_SET(confd, &set_rfd);
if(confd > max_rfd) max_rfd = confd;
}else{
//is connect socket
res = handle_op(fd);
if(res < 0){
FD_CLR(fd, &set_rfd);
close(fd);
}
}
}
}
}
close(listenfd);
return 0;
}

int business_rwaync_config(){
int res = -1;
op_files = seqlist_create();
if(NULL == op_files){
printf("op_files create fail\n");
return -1;
}
res = pthread_mutex_init(&op_files_mutex, NULL);
if(res < 0){
printf("op_files_mutex init fail\n");
return -1;
}
return 0;
}

int handle_op(int confd_t)
{
RequestBody req;
memset(&req, 0, SIZE_REQ);

int res = -1;
int num = read(confd_t, &req, SIZE_REQ);
if(num < 0)
{
printf("read %d error\n", confd_t);
return -2;
}
else if(num == 0)
{
printf("%d exit\n", confd_t);
return -1;
}

printf("%d:%s\n", confd_t, RequestTypeKey[req.type]);
//business handle
switch(req.type)
{
case none:
printf("%d:none\n", confd_t);
break;
case list:
{
handle_list(confd_t);
break;
}
case get:
{
FD_CLR(confd_t, &set_rfd);
pthread_t thr;
pthread_detach(thr);
ThreadArg *parg = (ThreadArg *)malloc(sizeof(ThreadArg));
parg->i_arg = confd_t;
strcpy(parg->s_arg, req.s_arg);
pthread_create(&thr, NULL, handle_get, parg);
break;
}
case put:
{
FD_CLR(confd_t, &set_rfd);
pthread_t thr;
pthread_detach(thr);
ThreadArg *parg = (ThreadArg *)malloc(sizeof(ThreadArg));
parg->i_arg = confd_t;
parg->i_arg2 = req.i_arg;
strcpy(parg->s_arg, req.s_arg);
pthread_create(&thr, NULL, handle_put, parg);
break;
}
case login:{
handle_login(confd_t, req.s_arg, req.s_arg2);
break;
}
case regist:{
handle_regist(confd_t, req.s_arg, req.s_arg2);
break;
}
case say:{
handle_say(confd_t, req.s_arg);
break;
}
default:{
printf("%d request error\n", confd_t);
break;
}
}
return 0;
}

int handle_list(int socket)
{
ResponeBody resp;
memset(&resp, 0, SIZE_RESP);

//query the file name in the directory
DIR *dir = opendir(PATH);
if(NULL == dir)
{
return -1;
}
struct dirent* rent = NULL;
while((rent = readdir(dir))!= NULL)
{
if(rent->d_name[0] == '.')
{
continue;
}
strcat(resp.data, rent->d_name);
strcat(resp.data, "\n");
}
int len = strlen(resp.data);
resp.data[len-1] = '\0';
resp.datalen = len-1;
closedir(dir);

//send result
resp.type = list;
resp.status = 1;
my_write(socket, &resp, SIZE_RESP);
printf("list success\n");
return 0;
}

bool condition_op_file(data_t* data, void *cmpvalue){
if(strcmp(data->name, cmpvalue) == 0){
return true;
}
return false;
}


void* handle_get(void *arg)
{
printf("get ----\n");
int res = -1;
void * ret = NULL;
ThreadArg* parg = (ThreadArg *)arg;
int socket = parg->i_arg;
if(socket <= 0){
printf("get -> socket error\n");
goto geterror;
}
ResponeBody resp;
resp.type = getData;
int len_get = 0,num=0;
memset(&resp, 0, SIZE_RESP);

//printf("%d:get\n", socket);

if(strlen(parg->s_arg) <= 0){
sprintf(resp.data, "please input filename");
my_write(socket, &resp, SIZE_RESP);
printf("get -> please input filename\n");
goto geterror;
}

//set file is read status
int index = -1;
pthread_mutex_lock(&op_files_mutex);
res = seqlist_find_bycondition(op_files, condition_op_file, parg->s_arg);
if(res < 0){
data_t data;
strcpy(data.name, parg->s_arg);
data.type = OP_READ;
index = seqlist_get_length(op_files);
seqlist_insert(op_files, index, data);
}else{
index = res;
data_t data = op_files->data[res];
if(data.type == OP_WRITE){//already write
// can not read
pthread_mutex_unlock(&op_files_mutex);
printf("get -> curr have user operating, please try again later\n");

resp.status = 0;
strcpy(resp.data, "curr have user operating, please try again later");
my_write(socket, &resp, SIZE_RESP);
goto geterror;
}
}
pthread_mutex_unlock(&op_files_mutex);

char path[50] = {0};
sprintf(path, "%s%s", PATH, parg->s_arg);
struct stat stat_t;
res = stat(path, &stat_t);
if(res < 0){
strcpy(resp.data, strerror(errno));
my_write(socket, &resp, SIZE_RESP);
printf("get -> stat error\n");
goto geterror;
}
if(stat_t.st_size <= 0){
sprintf(resp.data, "%s is empty", parg->s_arg);
my_write(socket, &resp, SIZE_RESP);
printf("get -> %s\n",resp.data);
goto geterror;
}
int fd = open(path, O_RDWR);
if(fd < 0)
{
strcpy(resp.data, strerror(errno));
my_write(socket, &resp, SIZE_RESP);
goto geterror;
}
resp.type = getData;
resp.status = 1;
resp.datatotal = stat_t.st_size;
my_write(socket, &resp, SIZE_RESP);

printf("get ++++ while\n");
while((num = read(fd, resp.data, SIZE_RESP_DATA)) > 0)
{
resp.type = getData;
resp.status = 1;
resp.datalen = num;
res = my_write(socket, &resp, SIZE_RESP);
if(res < 0){
printf("get ++++ res < 0\n");
goto geterror;
}
memset(&resp, 0, SIZE_RESP);
usleep(20);
}
close(fd);
geterror:
pthread_mutex_lock(&op_files_mutex);
op_files->data[res].type &= ~OP_READ;
pthread_mutex_unlock(&op_files_mutex);
FD_SET(parg->i_arg, &set_rfd);
free(parg);
printf("get ====\n");
return ret;
}

void* handle_put(void *arg)
{
void *ret = NULL;
ThreadArg *parg = (ThreadArg *)arg;
int socket = parg->i_arg;
char *filename = parg->s_arg;
int datatotal = parg->i_arg2;

if(NULL == filename || 0 == *filename){
ret = (void *)-1;
printf("put -> filename error\n");
goto puterror;
}

if(datatotal <= 0){
ret = (void *)-1;
printf("put -> datatotal <= 0\n");
goto puterror;
}
ResponeBody resp;
memset(&resp, 0, SIZE_RESP);
resp.type = putData;

//set file is write status
int index = -1;
pthread_mutex_lock(&op_files_mutex);
int res = seqlist_find_bycondition(op_files, condition_op_file, filename);
if(res < 0){
data_t data;
strcpy(data.name, filename);
data.type = OP_WRITE;
index = seqlist_get_length(op_files);
seqlist_insert(op_files, index, data);
}else{
index = res;
data_t data = op_files->data[res];
if(data.type != OP_NONE){
pthread_mutex_unlock(&op_files_mutex);
printf("put -> curr have user operating, please try again late\n");

resp.status = 0;
strcpy(resp.data, "curr have user operating, please try again later\n");
my_write(socket, &resp, SIZE_RESP);
ret = (void *)0;
goto puterror;
}
}
pthread_mutex_unlock(&op_files_mutex);

char path[50] = {0};
sprintf(path, "%s%s", PATH, filename);

int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
if(fd < 0){
ret = (void *)-2;
printf("put -> open error\n");
resp.status = 0;
strcpy(resp.data, "server put -> open file error\n");
my_write(socket, &resp, SIZE_RESP);
goto puterror;
}

resp.status = 1;
strcpy(resp.data, "data start\n");
my_write(socket, &resp, SIZE_RESP);

int len_curr_resp = resp.datalen + SIZE_RESP_INFO;
int num = 0;
int len_recv = 0;

while(1){
my_read(socket, (char *)&resp, SIZE_RESP);
if(resp.type != putData || 0 == resp.status){
printf("server put -> client resp config error\n");
ret = (void *)-2;
break;
}
write(fd, resp.data, resp.datalen);
len_recv += resp.datalen;
printf("server recv ----[%2.2f%%]----\n", len_recv/(double)datatotal * 100);
memset(&resp, 0, SIZE_RESP);
usleep(20);
if(len_recv >= datatotal){
printf("server recv success\n");
ret = (void *)0;
break;
}
}
close(fd);
puterror:
pthread_mutex_lock(&op_files_mutex);
op_files->data[res].type &= ~OP_WRITE;
pthread_mutex_unlock(&op_files_mutex);
FD_SET(socket, &set_rfd);
free(parg);
return ret;
}

int handle_login(int socket, const char *username, const char *userpwd){
ResponeBody resp;
memset(&resp, 0, SIZE_RESP);
resp.type = login;
resp.status = 1;
/*
my_write(socket, &resp, SIZE_RESP);
return 0;*/

if(username == NULL || '\0' == *username){
my_write(socket, &resp, SIZE_RESP);
return -1;
}
if(userpwd == NULL || '\0' == *userpwd){
my_write(socket, &resp, SIZE_RESP);
return -1;
}
int ret = -1, res = -1;
char sql[100] = {0};
sprintf(sql, "select count(*) from ftp_user where status=1 and username='%s' and userpwd='%s'",username, userpwd);
char **result;
int nrow=0, ncolumn=0;
char *errmsg = NULL;
res = sqlite3_get_table(db, sql, &result, &nrow, &ncolumn, &errmsg);
if(0 == res){
int count = atoi(*(result+ncolumn*1+0));
printf("count:%d\n", count);
if(count > 0){
resp.status = 1;
strcpy(resp.data, "login success");
res = my_write(socket, &resp, SIZE_RESP);
ret = 0;
}else{
resp.status = 0;
strcpy(resp.data, "username or userpwd error");
res = my_write(socket, &resp, SIZE_RESP);
ret = 0;
}
}else{
printf("errmsg:%s\n", errmsg);
strcpy(resp.data, errmsg);
my_write(socket, &resp, SIZE_RESP);
}
sqlite3_free_table(result);
return ret;
}

int handle_regist(int socket, const char *username, const char *userpwd){
ResponeBody resp;
memset(&resp, 0, SIZE_RESP);
resp.type = regist;
resp.status = 0;
if(username == NULL || '\0' == *username){
my_write(socket, &resp, SIZE_RESP);
return -1;
}
if(userpwd == NULL || '\0' == *userpwd){
my_write(socket, &resp, SIZE_RESP);
return -1;
}
int res = -1;
//query username is exist
char sql[100] = {0};
sprintf(sql, "select count(username) from ftp_user where username='%s'",username);
char **result;
int nrow=0, ncolumn=0;
char *errmsg = NULL;
res = sqlite3_get_table(db, sql, &result, &nrow, &ncolumn, &errmsg);
if(0 == res){
int count = atoi(*(result+ncolumn*1+0));
if(count > 0){
strcpy(resp.data, "username is exist");
res = my_write(socket, &resp, SIZE_RESP);
return 0;
}
}else{
printf("errmsg:%s\n", errmsg);
strcpy(resp.data, errmsg);
my_write(socket, &resp, SIZE_RESP);
return 0;
}
//insert record
sprintf(sql, "insert into ftp_user(username, userpwd) values('%s','%s')",username, userpwd);
res = sqlite3_exec(db, sql, NULL, NULL, &errmsg);
if(0 == res){
resp.status = 1;
strcpy(resp.data, "regist success");
res = my_write(socket, &resp, SIZE_RESP);
}else{
printf("errmsg:%s\n", errmsg);
strcpy(resp.data, errmsg);
my_write(socket, &resp, SIZE_RESP);
}
return 0;
}

int handle_say(int socket, const char *data){
printf("%d say: %s\n", socket, data);
return 0;
}

ftpclient.c
ftp客户端程序

/*************************************************************************
> File Name: telc.c
# File Name: telc.c
# Author :彭敬
# QQ : 784915651
# Email:784915651@qq.com
# Blog:http://blog.csdn.net/returningprodigal
# Created Time: 2017年12月06日 星期三 17时00分22秒
************************************************************************/


#include <stdio.h>
#include <signal.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <pthread.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <string.h>
#include <pthread.h>
#include <stdlib.h>

#include "common.h"
//file op
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>

//socket config
#include <sys/types.h>
#include <sys/socket.h>

//socket init
int my_socket_init(const char *ip, int port);
void sig_handle_sigusr1(int arg1);

//helper
int my_get_request(const char *str_req, char *type, char arg[40]);

//business handle
int handle_list(int socket);
int handle_get(int socket, const char *filename);
int handle_put(int socket, const char *filename);

int main(int argc, char *argv[])
{
/*
printf("req %d\n", sizeof(RequestBody));
printf("resp %d\n", sizeof(ResponeBody));
return 0;
*/


//recv user arg
/*
if(argc != 3){
printf("./ftpc <ip> <port>");
return -1;
}*/

//other config
signal(SIGUSR1, sig_handle_sigusr1);

//create socket
int confd = my_socket_init("127.0.0.1", 11223);
if(confd < 0)
{
perror("socket init error");
return -1;
}
printf("weclome to parkin ftp\n");

//user login
//my_login(confd);

//send user op
printf("****please input your op****\n");
char buff[100];
int num = -1, res = -1;
char arg[40] = {0};
char type = none;
while(1)
{
memset(buff, 0, 100);
num = read(0, buff, 100);
if(num <= 0){
continue;
}
res = my_get_request(buff, &type, arg);
if(res < 0){
printf("request error\n");
}
else if(get == type){
handle_get(confd, arg);
}
else if(put == type){
handle_put(confd, arg);
}
else if(list == type){
handle_list(confd);
}
else if(quit == type){
exit(0);
}
else if(clear == type){
system("clear");
}
}
close(confd);
}

int my_socket_init(const char *ip, int port){
int confd = socket(AF_INET, SOCK_STREAM, 0);
if(-1 == confd)
{
perror("client socket error");
return -1;
}
struct sockaddr_in addr;
addr.sin_family = AF_INET;
//addr.sin_port = htons(atoi(argv[2]));
//addr.sin_addr.s_addr = inet_addr(argv[1]);
//addr.sin_port = htons(atoi("11223"));
//addr.sin_addr.s_addr = inet_addr("127.0.0.1");
addr.sin_port = htons(port);
addr.sin_addr.s_addr = inet_addr(ip);
int res = connect(confd, (struct sockaddr *)&addr, sizeof(addr));
if(res < 0){
perror("connect fail\n");
return -1;
}
return confd;
}

int my_login(int socket){
int ret = -1;
RequestBody req;
memset(&req, 0, SIZE_REQ);
ResponeBody resp;
memset(&resp, 0, SIZE_RESP);
while(1){
//auth user login
printf("-----------------------------\n");
printf("--1.login 2.regist 3.quit--\n");
printf("-----------------------------\n");
char op = 0;
//setbuf(stdin, NULL);
scanf("%c",&op);
while(( getchar())!='\n');//clear stdio '\n' cache
switch(op){
case '1':{
req.type = login;
printf("please input user name:");
scanf("%s", req.s_arg);
printf("please input user pwd:");
scanf("%s", req.s_arg2);
my_write(socket, &req, SIZE_REQ);
my_read(socket, (char *)&resp, SIZE_RESP);
if(login == resp.type && 1 == resp.status){
printf("login success\n");
return 0;
}
printf("login fail:%s\n", resp.data);
break;
}
case '2':{
req.type = regist;
char userpwd_re[40] = {0};
printf("please input user name:");
scanf("%s", req.s_arg);
printf("please input user pwd:");
scanf("%s", req.s_arg2);
printf("please re-input user pwd:");
scanf("%s", userpwd_re);
if(strcmp(req.s_arg2, userpwd_re) == 0){
my_write(socket, &req, SIZE_REQ);
my_read(socket, (char *)&resp, SIZE_RESP);
if(regist == resp.type && 1 == resp.status){
printf("regist success\n");
continue;
}else{
printf("regist fail:%s\n", resp.data);
}
}else{
printf("password is different twice\n");
}
break;
}
case '3':{
exit(0);
}
default:{
printf("input error\n");
break;
}
}
memset(&req, 0, SIZE_REQ);
memset(&resp, 0, SIZE_RESP);
printf("please wait 3 sec...\n");
sleep(3);
system("clear");
}
return ret;
}

void sig_handle_sigusr1(int arg1){
//printf("sigusr\n");
}

int my_get_request(const char *str_req, char *type, char arg[40])
{
if(NULL == str_req || 0 == *str_req)
{
return -1;
}
char seg[] = " ";
char charlist[2][50] = {0};
int i = 0;
char *substr = strtok((char *)str_req, seg);
while (substr != NULL)
{
strcpy(charlist[i],substr);
i++;
substr = strtok(NULL,seg);
}
if(i<= 0 || i > 2)
{
return -1;
}
//\n replace \0
int len_1 = strlen(charlist[0]);
if(len_1 > 0 && '\n' == charlist[0][len_1-1])
{
charlist[0][len_1-1] = '\0';
}
int len_2 = strlen(charlist[1]);
if(len_2 > 0 && '\n' == charlist[1][len_2-1])
{
charlist[1][len_2-1] = '\0';
}

if(strcmp(charlist[0], "get") == 0)
{
*type = get;
}
else if(strcmp(charlist[0], "put") == 0)
{
*type = put;
}
else if(strcmp(charlist[0], "list") == 0)
{
*type = list;
}
else if(strcmp(charlist[0], "quit") == 0)
{
*type = quit;
}
else if(strcmp(charlist[0], "clear") == 0)
{
*type = clear;
}
else if(strcmp(charlist[0], "say") == 0)
{
*type = say;
}
else{
*type = none;
}
strcpy(arg, charlist[1]);
return 0;
}

int handle_list(int socket){
if(socket == 0){
printf("list socket is empty\n");
return 0;
}
RequestBody req;
memset(&req, 0, SIZE_REQ);
req.type = list;
my_write(socket, &req, SIZE_REQ);

ResponeBody resp;
memset(&resp, 0, SIZE_RESP);
my_read(socket, (char *)&resp, SIZE_RESP);
printf("%s\n", resp.data);
return 0;
}

int handle_get(int socket,const char *filename)
{
if(NULL == filename || 0 == *filename){
printf("get -> filename error");
return -1;
}

char path[50] = {0};
sprintf(path, "%s%s", PATH, filename);

int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
if(fd < 0){
perror("open error");
return -1;
}
RequestBody req;
memset(&req, 0, SIZE_REQ);
req.type = get;
strcpy(req.s_arg, filename);
my_write(socket, &req, SIZE_REQ);

ResponeBody resp;
memset(&resp, 0, SIZE_RESP);
int datatotal = 0;
my_read(socket, (char *)&resp, SIZE_RESP);
if(getData == resp.type){
if(1 == resp.status){
datatotal = resp.datatotal;
}else{
printf("getData -> %s\n", resp.data);
}
}
memset(&resp, 0, SIZE_RESP);

int num = 0,len_recv = 0;
//read data
int ret = -1;
while(1){
my_read(socket, (char *)&resp, SIZE_RESP);
if(0 == resp.status || resp.type != getData){
printf("getData -> resp config error\n");
ret = -2;
break;
}
write(fd, resp.data, resp.datalen);
len_recv += resp.datalen;
printf("get ----[%2.2f%%]----\n", len_recv/(double)datatotal * 100);
memset(&resp, 0, SIZE_RESP);
if(len_recv >= datatotal){
printf("get success\n");
ret = 0;
break;
}
}
close(fd);
//kill(getpid(), SIGUSR1);
return ret;
}

int handle_put(int socket, const char *filename)
{
int len_get = 0,num=0,res=-1;

char path[50] = {0};
sprintf(path, "%s%s", PATH, filename);
struct stat stat_t;
res = stat(path, &stat_t);
if(res < 0){
perror("stat error");
return -2;
}
if(stat_t.st_size <= 0){
printf("%s is empty\n", filename);
return -2;
}
int fd = open(path, O_RDWR);
if(fd < 0)
{
perror("open error");
return -2;
}

RequestBody req;
memset(&req, 0, SIZE_REQ);
req.type = put;
strcpy(req.s_arg, filename);
req.i_arg = stat_t.st_size;
res = my_write(socket, &req, SIZE_REQ);

ResponeBody resp;
memset(&resp, 0, SIZE_RESP);
my_read(socket, (char *)&resp, SIZE_RESP);
if(resp.type == putData){
if(0 == resp.status){
printf("putData -> %s\n", resp.data);
goto puterror;
}
}

while((num = read(fd, resp.data, SIZE_RESP_DATA)) > 0)
{
if(num <= 0){
perror("read error");
return -1;
}
resp.type = putData;
resp.status = 1;
resp.datalen = num;
res = my_write(socket, &resp, SIZE_RESP);
if(res < 0){
return -1;
}
memset(&resp, 0, SIZE_RESP);
//usleep(100);
}
puterror:
close(fd);
return 0;
}