概述
服务器:打开目录文件将文件名发送给客户端
get 1.c :下载文件 put 1.c :上传文件 list:列表:当前服务器下的文件
在这里只实现了list:获取列表
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
#define SIZE 64
//封装有关服务器的函数,方便调用
int server_init(char *ipaddr,unsigned short port,int backlog)//IP地址、端口号、监听最大数
{
int sockfd = socket(PF_INET,SOCK_STREAM,0);//网络编成接口
if(sockfd == -1)
{
perror("socket");
return -1;
}
// printf("sockfd:%dn",sockfd);
struct sockaddr_in saddr;
memset(&saddr,0,sizeof(saddr));//清空
saddr.sin_family = PF_INET;//填充地址信息,指定协议ipv4
saddr.sin_port = htons(port);//端口号
saddr.sin_addr.s_addr =(ipaddr == NULL)?htonl(INADDR_ANY): inet_addr(ipaddr);//ip地址,点分式转二进制网络字节序
int ret = bind(sockfd,(struct sockaddr *)&saddr,sizeof(saddr));//将服务器的IP,PORT与sockfd接口绑定绑定
if(ret == -1)
{
perror("bind");
close(sockfd);
return -1;
}
printf("bind successn");
ret = listen(sockfd,backlog);//监听是否有客户端请求连接
if(ret == -1)
{
perror("listen");
close(sockfd);
return -1;
}
return sockfd;
}
void processGet(int rws,char *filenName)//下载
{
}
void processPut(int rws,char *fileName)//上传
{
}
void processList(int rws)//目录列表
{
char buf[SIZE] = {0};
DIR *dp = opendir(".");//打开目录
if(dp == NULL)
{
perror("opendir");
return;
}
struct dirent *sdp = NULL;
while((sdp = readdir(dp)))//读取目录
{
if(!(strcmp(sdp->d_name,".")) || (!strcmp(sdp->d_name,"..")))
continue;
strcpy(buf,sdp->d_name);
if( -1== send(rws,buf,sizeof(buf),0))//文件名发送给客户端
{
perror("send");
break;
}
}
closedir(dp);
close(rws);
}
int main(int argc, char *argv[])
{
if(argc < 3)
{
printf("Usage:%s <ip><port>n",argv[0]);
return -1;
}
int sockfd = server_init(argv[1],atoi(argv[2]),1024);
if(sockfd == -1)
{
printf("server_init errorn");
return -1;
}
printf("listen...n");
char buf[SIZE] = {0};
while(1)
{
int rws = accept(sockfd,NULL,NULL);//连接
if(rws == -1)
{
perror("accept");
close(sockfd);
return -1;
}
printf("rws = %d n",rws);
//通信
bzero(buf,sizeof(buf));
int ret = recv(rws,buf,sizeof(buf),0);//读客户端请求
if(ret == -1)
{
perror("recv");
break;
}
else if(ret == 0)
{
printf("clint close");
close(rws);
continue;
}
//判断请求:G P L
switch(buf[0])
{
case 'G'://下载
processGet(rws,buf+1);
break;
case 'P'://上传
processPut(rws,buf+1);
break;
case 'L'://列表
processList(rws);
break;
}
}
close(sockfd);
return 0;
}
客户端:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define SIZE 64
void processList(struct sockaddr_in saddr)//列表
{
char buf[SIZE] = {0};
int sockfd = socket(PF_INET,SOCK_STREAM,0);
if(sockfd == -1)
{
perror("socket");
return;
}
int ret = connect(sockfd,(struct sockaddr *)&saddr,sizeof(saddr));//将服务器的IP> ,PORT与sockfd接口绑定绑定
if(ret == -1)
{
perror("connect");
close(sockfd);
return ;
}
//发送L
buf[0] = 'L';
if(send(sockfd,buf,sizeof(buf),0) == -1)
{
}
while(recv(sockfd,buf,sizeof(buf),0)>0)//循环接受文件名
{
printf("%s ",buf);
}
close(sockfd);
}
void processGet(struct sockaddr_in saddr,char *fileName)
{
}
void processPut(struct sockaddr_in saddr,char *fileName)
{
}
int main(int argc, char *argv[])
{
int sockfd = socket(PF_INET,SOCK_STREAM,0);//网络编成接口
if(sockfd == -1)
{
perror("socket");
return -1;
}
printf("sockfd:%dn",sockfd);
struct sockaddr_in saddr;
memset(&saddr,0,sizeof(saddr));//清空
saddr.sin_family = PF_INET;//填充地址信息,指定协议ipv4
saddr.sin_port = htons(8888);//端口号
saddr.sin_addr.s_addr = inet_addr("127.0.0.1");//ip地址,点分式转二进制网络字节序
//通信
char buf[SIZE]={0};
while(1)
{
printf("<client>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = '