程序源码
结构体
struct msg
{
int type;
int flag;
char buf[64];
char fname[15];
};
type:用于标识是显示内容,上传或下载
buf:用于存储文件内容
fname:用于存储文件名
客户端源码:
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<string.h>
#include<stdlib.h>
#define SEARCH 100
#define DOWN 200
#define END 500
#define FAIL 300
#define SUCCESS 400
#define UP 600
struct msg
{
int type;
int flag;
char buf[64];
char fname[15];
};
int search_serverdir(int sockfd,struct msg info)
{
info.type = SEARCH;
send(sockfd,&info,sizeof(info),0);
while(1)
{
recv(sockfd,&info,sizeof(info),0);
if(info.flag == END)
break;
printf("%s ",info.buf);
}
printf("\n");
return 0;
}
int ftp_download(int sockfd,struct msg info)
{
info.type = DOWN;
printf("请输入要下载的文件名:");
fgets(info.fname,sizeof(info.fname),stdin);
info.fname[strlen(info.fname) - 1] = '\0';
send(sockfd,&info,sizeof(info),0);
recv(sockfd,&info,sizeof(info),0);
if(info.flag == FAIL)
{
printf("服务器打开文件失败\n");
return -1;
}
if(info.flag == SUCCESS)
printf("服务器打开文件成功\n");
FILE *fp;
fp = fopen(info.fname,"w");
if(fp == NULL)
{
perror("fopen");
return -1;
}
int len;
while(1)