能够实现简单的文件上传和下载.
服务器端程序:
/*********************:****************************************************
> File Name: server.c
> Author:
> Mail:
> Created Time: Thu 11 Jun 2015 12:34:37 AM PDT
************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<strings.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netinet/ip.h> /* superset of previous */
#include<netinet/in.h>
#include<arpa/inet.h>
#include<errno.h>
#include<dirent.h>
#include<sys/stat.h>
#include<stddef.h>
void do_list(int connfd);
void do_get(int connfd,char *s);
void do_put(int connfd,char *s);
int main(int argc,const char *argv[])
{
char buff[1024];
int serverfd,connfd;
serverfd = socket(PF_INET,SOCK_STREAM,0);
if(serverfd ==-1)
{
perror("socket");
exit(EXIT_FAILURE);
}
struct sockaddr_in servaddr;
servaddr.sin_family = PF_INET;
servaddr.sin_port = htons(8888);
servaddr.sin_addr.s_addr = inet_addr("192.168.1.146");
if (bind(serverfd,(struct sockaddr*)&servaddr,sizeof(servaddr))==-1)
{
perror("bind");
exit(EXIT_FAILURE);
}
printf("bind success!\n");
if(listen(serverfd,8)==-1)
{
perror("listen");
exit(EXIT_FAILURE);
}
printf("listen...\n");
connfd = accept(serverfd,NULL,NULL);
if(connfd == -1)
{
perror("accept");
exit(EXIT_FAILURE);
}
printf("connected..\n");
while(1)
{
recv(connfd,buff,sizeof(buff),0);
printf("kehuduan:%s",buff);
if(strncmp(buff,"quit",4)==0)
{
printf("kehuduan xiaxianle\n");
//close(connfd);
break;
}
if(strncmp(buff,"list",4)==0)
{
do_list(connfd);
continue;
}
if(strncmp(buff,"get ",4)==0)
{
do_get(connfd,buff+4);
}
if(strncmp(buff,"put ",4)==0)
{
do_put(connfd,buff+4);
}
}
return 0;
}
void do_list(int connfd)
{
DIR *dp;
char buff2[1024];
struct dirent *ep;
struct stat st;
dp = opendir(".");
if(dp!=NULL)
{
while(ep = readdir(dp))
{
if(ep->d_name[0]!='.')
{
printf("d_name:%s\n",ep->d_name);
if(stat(ep->d_name,&st)!=-1)
{
strcat(buff2,ep->d_name);
strcat(buff2,"\n");
}
}
}
send(connfd,buff2,sizeof(buff2),0);
closedir(dp);
}
else
puts("Couldn't open directory.\n");
}
void do_get(int connfd,char *s)
{
DIR *dp;
struct dirent *ep;
struct stat st;
int total,sndbytes;
FILE *fp1;
char buff3[1024];
char buff4[1024];
bzero(buff3,sizeof(buff3));
bzero(buff4,sizeof(buff4));
strcat(buff3,s);
buff3[strlen(buff3)-1]='\0';
dp=opendir(".");
if(dp!=NULL)
{
while(ep=readdir(dp))
{
if(strcmp(buff3,ep->d_name)==0)
{
if(stat(ep->d_name,&st)!=-1)
{
total = st.st_size;
send(connfd,&total,sizeof(int),0);
printf("total = %d\n",total);
fp1 = fopen(ep->d_name,"r");
if(fp1==NULL)
{
printf("can not open!\n");
}
while(1)
{
if(total>=sizeof(buff4))
{
fread(buff4,1,sizeof(buff4),fp1);
sndbytes = send(connfd,buff4,sizeof(buff4),0);
total = total-sndbytes;
}
if(total<sizeof(buff4))
{
printf("haishengxia:%d\n",total);
fread(buff4,1,total,fp1);
send(connfd,buff4,total,0);
break;
}
}
}
}
}
closedir(dp);
}
else
puts("couldn't open the directory.\n");
}
void do_put(int connfd,char *s)
{
char buffer1[1024];
char filename[100];
bzero(filename,sizeof(filename));
FILE *fp2;
int total,recvbytes,num;
if(recv(connfd,&total,sizeof(int),0)==-1)
{
perror("recv");
exit(EXIT_FAILURE);
}
printf("shangchuan_size:%d\n",total);
strcat(filename,s);
filename[strlen(filename)-1] = '\0';
strcat(filename,"_input");
fp2 = fopen(filename,"w+");
while(1)
{
recvbytes = recv(connfd,buffer1,sizeof(buffer1),0);
fwrite(buffer1,1,sizeof(buffer1),fp2);
total = total - recvbytes;
if(total<sizeof(buffer1))
{
recvbytes = recv(connfd,buffer1,total,0);
fwrite(buffer1,1,recvbytes,fp2);
break;
}
}
}
客户端程序:
/*************************************************************************
> File Name: client.c
> Author:
> Mail:
> Created Time: Thu 11 Jun 2015 12:58:14 AM PDT
************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h> /* superset of previous */
#include <netinet/in.h>
#include <arpa/inet.h>
#include<errno.h>
#include<dirent.h>
#include<sys/stat.h>
#include<stddef.h>
#include<strings.h>
void list(int clientfd);
void put(int clientfd,char *s);
void get(int clientfd,char *s);
int main(int argc, const char *argv[])
{
int clientfd;
char buff[1024];
clientfd = socket(PF_INET,SOCK_STREAM,0);
if(clientfd == -1)
{
perror("socket");
exit(EXIT_FAILURE);
}
struct sockaddr_in servaddr;
servaddr.sin_family = PF_INET;
servaddr.sin_port = htons(8888);
servaddr.sin_addr.s_addr = inet_addr("192.168.1.146");
if(connect(clientfd,(struct sockaddr *)&servaddr,sizeof(servaddr))==-1)
{
perror("connect");
exit(EXIT_FAILURE);
}
printf("connect success!\n");
while(fgets(buff,sizeof(buff),stdin))
{
if( send(clientfd,buff,sizeof(buff),0)==-1)
{
perror("send");
exit(EXIT_FAILURE);
}
if(strncmp(buff,"help",4)==0)
{
printf("help:显示客户端所有命令和说明\n");
printf("list:显示服务器端可下载文件列表\n");
printf("get<file>:下载文件\n");
printf("put<file>:上传文件\n");
printf("quit:退出客户端\n");
}
if(strncmp(buff,"list",4)==0)
{
list(clientfd);
continue;
}
if(strncmp(buff,"quit",4)==0)
{
close(clientfd);
break;
}
if(strncmp(buff,"get ",4)==0)
{
get(clientfd,buff+4);
}
if(strncmp(buff,"put ",4)==0)
{
put(clientfd,buff+4);
}
}
return 0;
}
void list(int clientfd)
{
char buff2[1024];
if(recv(clientfd,buff2,sizeof(buff2),0)==-1)
{
perror("recv");
exit(EXIT_FAILURE);
}
printf("可下载文件:\n%s\n",buff2);
}
void get(int clientfd,char *s)
{
char buff4[1024];
char filename[100];
bzero(filename,sizeof(filename));
FILE *fp1;
int total,recvbytes,num;
if(recv(clientfd,&total,sizeof(int),0)==-1)
{
perror("recv");
exit(EXIT_FAILURE);
}
printf("total:%d\n",total);
strcat(filename,s);
filename[strlen(filename)-1] = '\0';
strcat(filename,"_copy");
printf("filename:%s\n",filename);
fp1 = fopen(filename,"w+");
//printf("101hang?\n");
while(1)
{
recvbytes = recv(clientfd,buff4,sizeof(buff4),0);
fwrite(buff4,1,sizeof(buff4),fp1);
total = total - recvbytes;
if(total<sizeof(buff4))
{
recvbytes = recv(clientfd,buff4,total,0);
fwrite(buff4,1,recvbytes,fp1);
break;
}
}
}
void put(int clientfd,char *s)
{
DIR *dp;
FILE *fp2;
struct dirent *ep;
struct stat st;
int total,sndbytes;
char buffer1[1024];
char buffer2[1024];
bzero(buffer1,sizeof(buffer1));
bzero(buffer2,sizeof(buffer2));
strcat(buffer1,s);
buffer1[strlen(buffer1)-1]='\0';
dp=opendir(".");
if(dp!=NULL)
{
while(ep=readdir(dp))
{
if(strcmp(buffer1,ep->d_name)==0)
{
if(stat(ep->d_name,&st)!=-1)
{
total = st.st_size;
send(clientfd,&total,sizeof(int),0);
printf("total bytes:%d\n",total);
fp2 = fopen(ep->d_name,"r");
if(fp2==NULL)
{
printf("can not open this file\n");
}
while(1)
{
if(total>=sizeof(buffer2))
{
fread(buffer2,1,sizeof(buffer2),fp2);
sndbytes = send(clientfd,buffer2,sizeof(buffer2),0);
total = total-sndbytes;
}
if(total<sizeof(buffer2))
{
printf("shengyu:%d\n",total);
fread(buffer2,1,total,fp2);
send(clientfd,buffer2,total,0);
break;
}
}
}
}
}
closedir(dp);
}
else
puts("can not open this directory\n");
}