server
int process_list(int newsockfd,char *buf) //设置要共享的目录
{
DIR * dirp;
int ret;
struct dirent * direntp;
dirp = opendir("./");//判断返回值
while( (direntp = readdir(dirp)) != NULL)
{
if(strncmp(direntp->d_name,".",1) == 0 ) continue;
strcpy(buf,direntp->d_name);
ret = write(newsockfd,buf,N);//判断返回值
}
close(newsockfd);
return 0;
}
int process_get(int newsockfd,char *buf)
{
int ret,fd,nbytes;
fd = open(buf+4,O_RDWR);
while( (nbytes= read(fd,buf,N)) > 0)
{
ret = write(newsockfd,buf,nbytes);//判断返回值
}
close(newsockfd);
return 0;
}
int process_put(int newsockfd,char *buf)
{
int ret,fd,nbytes;
fd = open(buf+4,O_RDWR|O_CREAT|O_TRUNC,0664);//判断返回值
while( (nbytes= read(newsockfd,buf,N)) > 0)
{
ret = write(fd,buf,nbytes);//判断返回值
}
close(newsockfd);
close(fd);
return 0;
}
int main(int argc, const char *argv[])
{
char buf[N] ={0};
int sockfd,ret,newsockfd;
struct sockaddr_in myaddr,client;
if(argc != 3)
{
printf(“运行程序时带入ip 和 port\n”);
exit(-1);
}
socklen_t addrlen = sizeof(client);
sockfd = socket(AF_INET,SOCK_STREAM,0);
printf(“sockfd=%d\n”,sockfd);
memset(&myaddr,0,sizeof(myaddr));
myaddr.sin_family = AF_INET ;//IPv4 Internet protocols
myaddr.sin_port = htons(atoi(argv[2]));
myaddr.sin_addr.s_addr = inet_addr(argv[1]);
ret = bind(sockfd,(struct sockaddr *)&myaddr,sizeof(myaddr)) ; // 给socket 一个固定的ip 和端口
ret = listen(sockfd,10);//判断返回值
while(1)
{
newsockfd = accept(sockfd,(struct sockaddr *)&client,&addrlen) ; //判断返回值
printf(“newsockfd = %d\n”,newsockfd);
ret = read(newsockfd,buf,N);//判断返回值
printf(“buf=%s\n”,buf);
if( strncmp(buf,“list”,4) == 0)
{
process_list(newsockfd,buf);
}
else if( strncmp(buf,“get”,3) == 0 )
{
process_get(newsockfd,buf);
}
else if( strncmp(buf,“put”,3) == 0 )
{
process_put(newsockfd,buf);
}
}
return 0;
}
client
int process_list(struct sockaddr_in *addr,char * buf)
{
int ret,sockfd,nbytes;
sockfd = socket(AF_INET,SOCK_STREAM,0);//判断返回值
ret = connect(sockfd,(struct sockaddr *)addr,sizeof(*addr)) ;//判断返回值
ret = write(sockfd,buf,N);//判断返回值
while( (nbytes= read(sockfd,buf,N)) > 0)
{
printf("%s\n",buf);
}
close(sockfd);
return 0;
}
int process_get(struct sockaddr_in *addr,char * buf)
{
int ret,sockfd,nbytes,fd;
sockfd = socket(AF_INET,SOCK_STREAM,0);//判断返回值
ret = connect(sockfd,(struct sockaddr *)addr,sizeof(*addr)) ;//判断返回值
ret = write(sockfd,buf,N);//判断返回值
fd = open(buf+4,O_RDWR|O_CREAT|O_TRUNC,0664);//判断返回值
while( (nbytes= read(sockfd,buf,N)) > 0)
{
ret = write(fd,buf,nbytes);//判断返回值
}
close(sockfd);
return 0;
}
int process_put(struct sockaddr_in *addr,char * buf)
{
int ret,sockfd,nbytes,fd;
sockfd = socket(AF_INET,SOCK_STREAM,0);//判断返回值
ret = connect(sockfd,(struct sockaddr *)addr,sizeof(*addr)) ;//判断返回值
ret = write(sockfd,buf,N);//判断返回值
fd = open(buf+4,O_RDWR);//判断返回值
while( (nbytes= read(fd,buf,N)) > 0)
{
ret = write(sockfd,buf,nbytes);//判断返回值
}
close(sockfd);
close(fd);
return 0;
}
int main(int argc, const char *argv[])
{ char buf[N] = {0};
int sockfd,ret;
struct sockaddr_in server,client;
if(argc != 3)
{
printf(“运行程序时带入ip 和 port\n”);
exit(-1);
}
memset(&server,0,sizeof(server));
server.sin_family = AF_INET ;
server.sin_port = htons( atoi(argv[2]) );
server.sin_addr.s_addr = inet_addr(argv[1]);
while(1)
{
printf(">:");
fgets(buf,N,stdin);
buf[strlen(buf)-1] = ‘\0’;
if( strncmp(buf,“list”,4) == 0 )
{
process_list(&server,buf);
}
else if( strncmp(buf,“get”,3) == 0 )
{
process_get(&server,buf);
}
else if( strncmp(buf,“put”,3) == 0 )
{
process_put(&server,buf);
}
else if( strncmp(buf,“quit”,4) == 0 )
{
break;
}
else if( strncmp(buf,“exit”,4) == 0 )
{
break;
}
else
{
printf(“命令错误,请重新输入\n”);
}
}
return 0;
}