![]() |
#2
kob2022-05-31 14:59
|
代码:
服务器端:

else if(strcmp(text,"GET") == 0)
{
int fd;
char buf1[N] = "";
char text1[N] = "";
if(recv(acceptfd,text1,N,0) == -1)
{
perror("fail to recv");
exit(1);
}
if((fd = open(text1,O_RDONLY)) == -1)//表示以只读的方式打开文件
//open成功则返回,否则返回-1;
{
if(errno == ENOENT)
{
strcpy(buf1,"***NO FILE***");//strcpy函数,将后者复制到前者,返回值为指向前者的一个指针
if(send(acceptfd,buf1,N,0) == -1)
{
perror("fail to send");
exit(1);
}
return;
}
else
{
perror("fail to open");
exit(1);
}
}
//文件存在
strcpy(buf1,"***start***");
if(send(acceptfd,buf1,N,0) == -1)
{
perror("fail to send");
exit(1);
}
ssize_t bytes;
while((bytes = read(fd,buf1,N)) != 0)
{
if(send(acceptfd,buf1,bytes,0) == -1)
{
perror("fail to send");
exit(1);
}
}
sleep(1);
strcpy(buf1,"***over***");
if(send(acceptfd,buf1,N,0) == -1)
{
perror("fail to send");
exit(1);
}
printf("--文件发送完毕--\n");
}
{
int fd;
char buf1[N] = "";
char text1[N] = "";
if(recv(acceptfd,text1,N,0) == -1)
{
perror("fail to recv");
exit(1);
}
if((fd = open(text1,O_RDONLY)) == -1)//表示以只读的方式打开文件
//open成功则返回,否则返回-1;
{
if(errno == ENOENT)
{
strcpy(buf1,"***NO FILE***");//strcpy函数,将后者复制到前者,返回值为指向前者的一个指针
if(send(acceptfd,buf1,N,0) == -1)
{
perror("fail to send");
exit(1);
}
return;
}
else
{
perror("fail to open");
exit(1);
}
}
//文件存在
strcpy(buf1,"***start***");
if(send(acceptfd,buf1,N,0) == -1)
{
perror("fail to send");
exit(1);
}
ssize_t bytes;
while((bytes = read(fd,buf1,N)) != 0)
{
if(send(acceptfd,buf1,bytes,0) == -1)
{
perror("fail to send");
exit(1);
}
}
sleep(1);
strcpy(buf1,"***over***");
if(send(acceptfd,buf1,N,0) == -1)
{
perror("fail to send");
exit(1);
}
printf("--文件发送完毕--\n");
}
客户端:

strcpy(buf,"GET");
if(send(sockfd,buf,N,0) == -1)
{
perror("fail to send");
exit(1);
}
printf("请输入你想要的文件名:\n");
char file_name[N] = "";
fgets(file_name,N,stdin);
file_name[strlen(file_name) - 1] = '\0';
if(send(sockfd,file_name,N,0) == -1)
{
perror("fail to send");
exit(1);
}
int fd;
char buf1[N] = "";
char text1[N] = "";
if(recv(sockfd,text1,N,0) == -1)
{
perror("fail to recv");
exit(1);
}
if(strncmp(text1,"***NO FILE***",13) == 0)//将前者与后者进行比较,最多比较13个
{
printf("%s\n",text1);
}
else
{
printf("%s\n",text1);
if((fd = open("copy.txt",O_CREAT | O_TRUNC | O_WRONLY,0664)) == -1)
{
perror("fail to open");
exit(1);
}
ssize_t bytes;
while((bytes = recv(sockfd,text1,N,0)) != -1)
{
if(strncmp(text1,"***over***",10) == 0)
{
printf("%s\n",text1);
break;
}
write(fd,text1,bytes);
}
printf("--文件接收完毕--\n");
//return;
}
}
if(send(sockfd,buf,N,0) == -1)
{
perror("fail to send");
exit(1);
}
printf("请输入你想要的文件名:\n");
char file_name[N] = "";
fgets(file_name,N,stdin);
file_name[strlen(file_name) - 1] = '\0';
if(send(sockfd,file_name,N,0) == -1)
{
perror("fail to send");
exit(1);
}
int fd;
char buf1[N] = "";
char text1[N] = "";
if(recv(sockfd,text1,N,0) == -1)
{
perror("fail to recv");
exit(1);
}
if(strncmp(text1,"***NO FILE***",13) == 0)//将前者与后者进行比较,最多比较13个
{
printf("%s\n",text1);
}
else
{
printf("%s\n",text1);
if((fd = open("copy.txt",O_CREAT | O_TRUNC | O_WRONLY,0664)) == -1)
{
perror("fail to open");
exit(1);
}
ssize_t bytes;
while((bytes = recv(sockfd,text1,N,0)) != -1)
{
if(strncmp(text1,"***over***",10) == 0)
{
printf("%s\n",text1);
break;
}
write(fd,text1,bytes);
}
printf("--文件接收完毕--\n");
//return;
}
}