📄 fileserver.c
字号:
/**********************FileServer.c******************************/#include<stdio.h>#include<stdlib.h>#include <unistd.h>#include<string.h>#include<sys/types.h>#include<sys/fcntl.h>#include <sys/stat.h>#include<sys/socket.h>#include<netinet/in.h>#include<netdb.h>#include <dirent.h>#include <time.h>#define command "clear"//这里其实应改写成ifdef _WIN32 define command "cls" else define "clear",更合理些#include<errno.h>#define BUFFER_SIZE 4096#define NAME_SIZE 200#define SERVER_PORT 1810#define TYPE_FILE 8int main(void){ char c= 0; char buf[BUFFER_SIZE];//读文件的缓冲 char file[NAME_SIZE];//文件名 int fromlen = 0,source = 0,on=1; register int k,s,ns; struct sockaddr_in sin; struct hostent * hp; DIR *pdir; struct dirent *pfile; time_t tend,tstart; //clock_t tend,tstart; long timeess=0; long long files_size=0; unsigned int trans_count=0,i; unsigned int jmp_count,files_num; struct stat pfile_buf; char *file_path="/home/xianguohuang/ftpfile",pn[255]; //system(command);//清屏/*build socket connecting*/ if( (s = socket(AF_INET, SOCK_STREAM, 0)) < 0) { printf("获取SOCKET号失败!!!"); exit(-1); } setsockopt(s,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on)); //设定地址重用 printf("sock success!, sockfd=%d\n",s); sin.sin_family=AF_INET; sin.sin_port=htons(SERVER_PORT); sin.sin_addr.s_addr=INADDR_ANY; bzero(&(sin.sin_zero),8); //printf("the s_addr is:%d\n",sin.sin_addr.s_addr); //memcpy(&sin.sin_addr, hp->h_addr, hp->h_length);/*bind the socket to the address*/ if(bind(s, (struct sockaddr*)&sin, sizeof(sin)) < 0 ) { perror("不能将服务器地址捆绑到SOCKET号上!!!\n"); close(s); exit(-1); }/*listenning the port*/ if(listen(s,20) < 0) { fprintf(stderr, "sever:listen\n"); close(s); exit(-1); } memset(pn,0,sizeof(pn)); printf("sizeof pn%d",sizeof(pn)); if ((pdir=opendir(file_path))== NULL) { printf("can't open directory"); close(s); exit(-1); } printf("准备传输目录文件\n"); tstart=time(NULL); printf("the current time is:%dl\n",tstart);//遍历获得准备传输的文件数目及总size while((pfile=readdir(pdir))!= NULL) { if(!strcmp("..", pfile->d_name) || !strcmp(".", pfile->d_name)) { jmp_count++; continue; } if(pfile->d_type!=TYPE_FILE) { jmp_count++; continue;//不为文件,则跳出 } sprintf(pn,"%s%s%s",file_path,"/",pfile->d_name); //strcat(pn,pfile->d_name); if(stat(pn,&pfile_buf)<0) { perror("获取文件信息失败\n"); exit(-1); } //if((S_ISREG(pfile_buf.st_mode)) && (strcmp(pfile->d_name,".") != 0) && (strcmp(pfile->d_name,"..")!=0)) { files_num++; files_size+=pfile_buf.st_size; } /*else { continue; }*/ printf("the pn string is :%s\n",pn); } printf("准备传输的文件数目为%d\n",files_num); printf("文件所需空间为:%d\n",files_size); if(closedir(pdir)<0) { printf("关闭文件夹失败\n"); exit (-1); } //------------------------------------------------------------------------------- for(i=0;i<255;i++) { pn[i]=0; } if ((pdir=opendir(file_path))== NULL) { printf("can't open directory"); close(s); exit(1); } while((pfile=readdir(pdir))!= NULL) { //if(pfile->d_ino==0) continue; //文件已删除 if(!strcmp("..", pfile->d_name) || !strcmp(".", pfile->d_name)) { //jmp_count++; continue; } if(pfile->d_type!=TYPE_FILE) { //jmp_count++; continue;//不为文件,则跳出 } sprintf(pn,"%s%s%s",file_path,"/",pfile->d_name); //此处加入搜索目录文件的代码 //printf("等待传送文件中.......\n"); /*传输文件*/ printf("等待客户机请求\n"); if((ns = accept(s, NULL, NULL)) < 0 ) { fprintf(stderr, "sever:accept\n"); exit(6); } printf("输入要传输的文件名:"); memset(file, 0, NAME_SIZE); sprintf(file,pfile->d_name); //printf("the file type code is:%d\n",pfile->d_type); printf("准备传输文件%s\n",pn); //scanf("%s",file); if((source = open(pn, O_RDONLY)) < 0) { perror("源文件打开出错"); close(s); exit(1); } //tstart=clock(); lseek(source,0L,0); printf("开始传送文件.....\n"); //先把文件名发过求 write(ns,file,sizeof(file)); printf("%s\n",pn); //发送文件名 memset(buf, 0, BUFFER_SIZE); while((k = read(source, buf, sizeof(buf))) > 0) { //循环发送直到,文件结束 write(ns,buf,k); memset(buf, 0, BUFFER_SIZE); } printf("\n传输完毕\n"); //tend=clock(); //timeess+=(double)(tend-tstart)/CLOCKS_PER_SEC; trans_count++; close(source); } tend=time(NULL); printf("the current time is:%d\n",tend); timeess=(long)(tend-tstart); //timeess=(tend.tm_sec)-(tstart.tm_sec); closedir(pdir); if((ns = accept(s, NULL, NULL)) < 0 ) { fprintf(stderr, "sever:accept\n"); exit(6); } write(ns,&files_size,sizeof(files_size)); printf("the files_size is:%ld\n",files_size); write(ns,&files_num,sizeof(files_num)); printf("the files_num is:%d\n",files_num); close(s); printf("\n传输完成,共用时****%d秒****\n",timeess); printf("共传输****%d****个文件\n",trans_count); printf("共跳过%d个文件或目录\n",jmp_count); exit(0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -