資源簡介
這是一個簡易的FTP系統,目前實現的功能有:
1、查看工作目錄
2、更換工作目錄
3、多線程下載文件
4、上傳文件
5、簡易的斷點續傳
6、簡易的用戶權限管理
7、支持多客戶端同時訪問服務器

代碼片段和文件信息
#include?“client.h“
int?sock_fd;
int?read_reply()
{
int?ret_code?=?0;
if?(recv(sock_fd?&ret_code?sizeof(ret_code)?0)?0)?
{
perror(“client:?error?reading?message?from?server\n“);
return?-1;
}
return?ntohl(ret_code);
}
void?print_reply(int?rc)?
{
switch?(rc)
{
case?220:
printf(“Welcome.\n“);
break;
case?221:
printf(“Thanks?you!\n“);
break;
case?226:
printf(“Requested?file?action?successful.\n“);
break;
case?550:
printf(“File?unavailable.\n“);
break;
}
}
int?client_read_command(char?*buf?int?size?char?*arg?char?*code)
{
bzero(arg?sizeof(arg));
bzero(code?sizeof(code));
printf(“client>?“);
fflush(stdout);
read_input(buf?size);
char?*temp_arg?=?NULL;
temp_arg?=?strtok(buf?“?“);
temp_arg?=?strtok(NULL?“?“);
if(temp_arg?!=?NULL)
{
strncpy(arg?temp_arg?strlen(temp_arg)?+?1);
}
if(strcmp(buf?“list“)?==?0)
{
strcpy(code?“LIST“);
}
else?if(strcmp(buf?“get“)?==?0)
{
strcpy(code?“RETR“);
}
else?if(strcmp(buf?“quit“)?==?0)
{
strcpy(code?“QUIT“);
}
else?if(strcmp(buf?“put“)?==?0)
{
strcpy(code?“STOR“);
}
else?if(strcmp(buf?“cd“)?==?0)
{
strcpy(code?“CWD“);
}
else?if(strcmp(buf?“rest“)?==?0)
{
strcpy(code?“REST“);
}
else
return?-1;
bzero(buf?sizeof(buf));
strcpy(buf?code);
if(temp_arg?!=?NULL)
{
strcat(buf?“?“);
strncat(buf?arg?strlen(arg)?+?1);
}
return?0;
}
int?client_get(int?work_fd?char?*arg)
{
char?data[MAX_SIZE];
int?recv_sizefile_size;
printf(“filename:%s\n“?arg);
int?fd?=?open(arg?O_RDWR|O_CREAT);
int?ret?=?recv(work_fd&file_sizesizeof(file_size)0);
if(recv_size?0)
{
perror(“recv?file_size?error\n“);
}
file_size?-=?lseekSize;
ret?=?send(work_fd&lseekSizesizeof(lseekSize)0);
if(ret?0)
{
perror(“send?lseekSize?error\n“);
}
int?currentSize?=?MAX_SIZE;
int?thread_numbers?=?file_size/MAX_SIZE?+?1;
printf(“%d\n“?file_size);
struct?thread_block?*blocks?=?(struct?thread_block?*)malloc(sizeof(struct?thread_block)?*?thread_numbers);
????int?i;
????for(i?=?0;?i????? blocks[i].number?=?i;
???? blocks[i].fd?=?fd;
???? blocks[i].work_fd?=?work_fd;
???? blocks[i].start?=i?*?currentSize;
???? blocks[i].end?=?blocks[i].start?+?currentSize;
????}
????blocks[thread_numbers-1].end?=?file_size;
????pthread_t?ptid[thread_numbers];
????//創建線程
????for(int?i?=?0;i????? pthread_create(&ptid[i]NULLthreadfunction&blocks[i]);
????}
????//線程join
????for(i?=?0;i????? pthread_join(ptid[i]NULL);
????}
????free(blocks);
????close(fd);
????lseekSize?=?0;//偏移量置零
return?0;
}
void?*threadfunction(void?*argv){
struct?thread_block?*block?=?(struct?thread_block?*)argv;
char?buf[MAX_SIZE];
int?ret;
printf(“In?thread?%dstart?=?%d??end?=?%d?\n“block->numberblock->startblock->end);
????//發送偏移量
????ret?=?send(block->work_fd?&block->end?sizeof(block->end)?0);
????if(ret?0)
{
perror(“send?lseek_size?error“);
exit(1);
}
????int?rec
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件?????????11??2018-06-30?09:55??FTP\client\1.txt
?????文件??????50632??2018-06-29?15:55??FTP\client\client
?????文件???????9378??2018-06-30?07:59??FTP\client\client.c
?????文件???????1047??2018-06-29?19:26??FTP\client\client.h
?????文件??????61536??2018-06-29?15:55??FTP\client\client.o
?????文件????????250??2018-06-28?20:44??FTP\client\makefile
?????文件??????12288??2018-06-30?10:08??FTP\client\server.c
?????文件???????5984??2018-06-28?09:34??FTP\common\-lpthread
?????文件???????2390??2018-06-30?07:51??FTP\common\common.c
?????文件????????894??2018-06-28?09:24??FTP\common\common.h
?????文件???????5984??2018-06-28?09:26??FTP\common\common.o
?????文件????????855??2018-07-01?13:00??FTP\README.md
?????文件?????103056??2018-06-28?09:29??FTP\server\-pthread
?????文件?????????46??2018-06-29?22:50??FTP\server\.passwd
?????文件?????????31??2018-06-30?00:20??FTP\server\.userpermission
?????文件?????????11??2018-06-29?15:12??FTP\server\1.txt
?????文件?????????45??2018-06-09?15:50??FTP\server\code.tar.gz
?????文件????????250??2018-06-28?15:35??FTP\server\makefile
?????文件??????84840??2018-06-30?00:12??FTP\server\server
?????文件??????11502??2018-06-30?00:12??FTP\server\server.c
?????文件???????1439??2018-06-29?23:54??FTP\server\server.h
?????文件?????105800??2018-06-30?00:12??FTP\server\server.o
?????目錄??????????0??2018-07-01?13:00??FTP\client
?????目錄??????????0??2018-06-30?11:14??FTP\common
?????目錄??????????0??2018-06-30?11:14??FTP\server
?????目錄??????????0??2018-06-30?11:14??FTP
-----------?---------??----------?-----??----
???????????????458269????????????????????26
評論
共有 條評論
- 頂一次
- 踩一次 1400 次