資源簡介
在linux中,使用tcp實現(xiàn)一個簡單的文件服務器功能,可以查看、上傳、下載文件

代碼片段和文件信息
#include???//printf
#include???//inet_addr?htons
#include?
#include???//socket?bind?listen?accept?connect
#include???//sockaddr_in
#include???//exit
#include???//close
#include?
#include?
#include?
#include?
#define?N?128
#define?errlog(errmsg)?do{\
perror(errmsg);\
printf(“%s?-->?%s?-->?%d\n“?__FILE__?__func__?__LINE__);\
exit(1);\
?}while(0)
void?do_help()
{
printf(“*****************************************************\n“);
printf(“*****?????????輸入??/???功能??????????***************\n“);
printf(“*****?????????list??/???查看服務器所在目錄的文件*****\n“);
printf(“*****?get?filename??/???下載服務器所在目錄的文件*****\n“);
printf(“*****?put?filename??/???上傳文件到服務器*************\n“);
printf(“*****?????????quit??/???退出??????????***************\n“);
printf(“*****************************************************\n“);
return?;
}
void?do_list(int?sockfd)
{
char?buf[N]?=?{};
//告知服務器執(zhí)行查看目錄文件名的功能
strcpy(buf?“L“);
send(sockfd?buf?N?0);
//接收數(shù)據(jù)并打印
while(1)
{
recv(sockfd?buf?N?0);
//結(jié)束標志
if(strncmp(buf?“OVER****“?8)?==?0)
{
break;
}
printf(“***?%s\n“?buf);
}
printf(“文件接收完畢\n“);
return?;
}
int?do_download(int?sockfd?char?*filename)
{
char?buf[N]?=?{};
int?fd;
ssize_t?bytes;
//發(fā)送指令以及文件名,告知服務器實現(xiàn)下載的功能
sprintf(buf?“G?%s“?filename);
send(sockfd?buf?N?0);
//接收數(shù)據(jù)判斷文件是否存在
recv(sockfd?buf?N?0);
//如果文件不存在,打印指令
if(strncmp(buf?“NO“?2)?==?0)
{
printf(“文件%s不存在,請重新輸入\n“?filename);
return?-1;
}
//如果文件存在,創(chuàng)建文件
if((fd?=?open(filename?O_CREAT?|?O_WRONLY?|?O_TRUNC?0664))?0)
{
errlog(“fail?to?open“);
}
//接收數(shù)據(jù)并寫入文件
while((bytes?=?recv(sockfd?buf?N?0))?>?0)
{
if(strncmp(buf?“OVER****“?8)?==?0)
{
break;
}
write(fd?buf?bytes);
}
printf(“文件下載完畢\n“);
return?-1;
}
int?do_upload(int?sockfd?char?*filename)
{
char?buf[N]?=?{};
int?fd;
ssize_t?bytes;
//打開文件,判斷文件是否存在
if((fd?=?open(filename?O_RDONLY))?0)
{
//如果文件不存在,則退出函數(shù),重新輸入
if(errno?==?ENOENT)
{
printf(“文件%s不存在,請重新輸入\n“?filename);
return?-1;
}
else
{
errlog(“fail?to?open“);
}
}
//如果文件存在,告知服務器執(zhí)行上傳的功能
sprintf(buf?“P?%s“?filename);
send(sockfd?buf?N?0);
//讀取文件內(nèi)容并發(fā)送
while((bytes?=?read(fd?buf?N))?>?0)
{
send(sockfd?buf?bytes?0);
}
sleep(1);
strcpy(buf?“OVER****“);
send(sockfd?buf?N?0);
printf(“文件上傳完畢\n“);
return?0;
}
int?main(int?argc?const?char?*argv[])
{
int?sockfd;
struct?sockaddr_in?serveraddr;
socklen_t?addrlen?=?sizeof(serveraddr);
char?buf[N]?=?{};
if(argc?3)
{
printf(“您輸入的參數(shù)太少了:?%s??\n“?argv[0]);
exit(1);
}
//第一步:創(chuàng)建套接字
if((sockfd?=?socket(AF_INET?SOCK_STREAM?0))?0)
{
errlog(“fail?to?socket“);
}
//第二步:填充服務器網(wǎng)絡信息結(jié)構(gòu)體
//inet_addr:將點分十進制ip地址轉(zhuǎn)化為網(wǎng)絡字節(jié)序的整型數(shù)據(jù)
//htons:將主機字節(jié)序轉(zhuǎn)化為網(wǎng)絡字節(jié)序
//atoi:將數(shù)字型字符串轉(zhuǎn)化為整型數(shù)據(jù)
serveraddr.sin_family?=?AF_INET;
serveraddr.sin_addr.s_addr?=?inet_addr(argv[1]);
server
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????205??2017-10-28?09:29??1_tcp_ftp\99.txt
?????文件???????4611??2017-10-28?11:24??1_tcp_ftp\client.c
?????文件????????205??2017-10-28?11:24??1_tcp_ftp\Makefile
?????文件???????4175??2017-10-28?11:24??1_tcp_ftp\server.c
?????目錄??????????0??2017-10-28?11:25??1_tcp_ftp
-----------?---------??----------?-----??----
?????????????????9196????????????????????5
- 上一篇:矩陣變換器
- 下一篇:基于RS485總線的PC與單片機多機通信系統(tǒng)設計
評論
共有 條評論