看看服务器这块,已经实现了单个文件的下载,那么一个文件目录下载怎么回事??
//service#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <dirent.h>
#define SER_IP_ADDR "192.168.1.107"
#define SER_PORT 9999
#define BACKLOG 12
int send_file(int new_sockfd)
{
char filename[56];
char buf[256];
int rn;
rn=read(new_sockfd,filename,sizeof(filename)-1);
filename[rn]=0;
printf("%s %d filename: %s\n",__func__,__LINE__,filename);
char pathfile[256];
sprintf(pathfile,"%s/%s","/home/smb/picture",filename);
FILE *fd;
char *per="No such file!";
fd=fopen(pathfile,"rb");
if(fd == NULL)
{
write(new_sockfd,per,strlen(per));
printf("[%s] %s\n",filename,per);
close(new_sockfd);
return -1;
}
char *pd="start download!";
write(new_sockfd,pd,strlen(pd));
while(1)
{
rn=fread(buf,1,sizeof(buf),fd);
if(rn<0)
{
perror("read file failed!\n");
return -1;
}
else if(rn==0)
break;
else
send(new_sockfd,buf,rn,0);
}
printf("send %s sucess!\n",filename);
close(new_sockfd);
return 0;
}
int main(int argc,char *argv[])
{
int socket_fd;
socket_fd=socket(AF_INET,SOCK_STREAM,0);
if(socket_fd<0)
{
perror("");
return -1;
}
struct sockaddr_in addr;
addr.sin_family=AF_INET;
addr.sin_port=htons(SER_PORT);
addr.sin_addr.s_addr=inet_addr(SER_IP_ADDR);
if(bind(socket_fd,(struct sockaddr*)&addr,sizeof(addr))<0)
{
perror("");
return -1;
}
if(listen(socket_fd,BACKLOG)<0)
{
perror("[listen]");
return -1;
}
int new_sockfd;
struct sockaddr_in c_addr;
socklen_t c_len=sizeof(c_addr);
while(1)
{
new_sockfd=accept(socket_fd,(struct sockaddr *)&c_addr,&c_len);
if(new_sockfd<0)
{
perror("");
return -1;
}
send_file(new_sockfd);
}
close(socket_fd);
return 0;
}
//client
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#define SER_IP_ADDR "192.168.1.107"
#define SER_PORT 9999
int recv_file(int sockfd,char *filename,char *buf)
{
FILE *fd;
fd=fopen(filename,"wb+");
if(fd == NULL)
{
perror("create file faile");
return -1;
}
int rn;
while(1)
{
rn=read(sockfd,buf,sizeof(buf));
if(rn==0)break;
fwrite(buf,1,rn,fd);
printf("30********");
}
printf("recv %s sucess!\n",filename);
fclose(fd);
return 0;
}
void send_req(int sockfd,char *filename)
{
int rn;
char buf[256];
send(sockfd,filename,strlen(filename),0);
rn=read(sockfd,buf,sizeof(buf)-1);
buf[rn]=0;
printf("[%d][%s]\n",__LINE__,buf);
if(strcmp(buf,"No such file!")==0)
{
printf("%s\n",buf);
return;
}
else if(strcmp(buf,"start download!")==0)
recv_file(sockfd,filename,buf);
else
printf("unknow err!\n");
}
int land(char *name,char *key)
{
if((strcmp(name,"admin")==0)&&(strcmp(key,"admin")==0))
{
return 1;
}
return -1;
}
int main(int argc,char *argv[])
{
int socket_fd;
socket_fd=socket(AF_INET,SOCK_STREAM,0);
if(socket_fd<0)
{
perror("");
return -1;
}
struct sockaddr_in addr;
addr.sin_family=AF_INET;
addr.sin_port=htons(SER_PORT);
addr.sin_addr.s_addr=inet_addr(SER_IP_ADDR);
if(connect(socket_fd,(struct sockaddr *)&addr,sizeof(addr))<0)
{
perror("[cliect connect]");
return -1;
}
char name[40]={0};
char key[40]={0};
loop:
printf("please input name:\n");
scanf("%s",name);
printf("please input key:\n");
scanf("%s",key);
int ch;
if((ch = (land(name,key))) == 1)
{
send_req(socket_fd,argv[1]);
}
else
{
printf("wrong\n");
sleep(1);
goto loop;
}
close(socket_fd);
return 0;
}