#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <unistd.h>
#include <libgen.h>
#include <signal.h>
#define portnumber 3333
void tcp_init(int *sockfd,int *new_fd,struct sockaddr_in server_addr,struct sockaddr_in client_addr){
if((*(sockfd)=socket(AF_INET,SOCK_STREAM,0))==-1)
{
fprintf(stderr,"Socket error:%s\n\a",strerror(errno));
exit(1);
}
bzero(&server_addr,sizeof(struct sockaddr_in));
server_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr=htonl(INADDR_ANY);
server_addr.sin_port=htons(portnumber);
if(bind(*(sockfd),(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1)
{
fprintf(stderr,"Bind error:%s\n\a",strerror(errno));
exit(1);
}
if(listen(*(sockfd),5)==-1)
{
fprintf(stderr,"Listen error:%s\n\a",strerror(errno));
exit(1);
}
int sin_size=sizeof(struct sockaddr_in);
if((*(new_fd)=accept(*(sockfd),(struct sockaddr *) (&client_addr),&sin_size))==-1)
{
fprintf(stderr,"Accept error:%s\n\a",strerror(errno));
exit(1);
}
fprintf(stderr,"Server get connection from %s\n",inet_ntoa (client_addr.sin_addr));
}
void upload(int sockfd,int new_fd)
{
char file_path[100] = "/home/cuiwei/Desktop/he/";
int nread=0;
char file_name[100];
int nbytes=0;
char buffer[1024];
int file_fd;
nread=read(new_fd,buffer,1024);
bzero(file_name,100);
printf("the begin file path is %s\n",file_path);
strcpy(file_name,buffer);
printf("file_name:%s\n",file_name);
bzero(buffer,1024);
printf("heihei\n");
strcat(file_path,file_name);
// strcat(file_path,"a.txt");
printf("This is the file_path:%s\n",file_path);
file_fd=open(file_path, O_RDWR|O_CREAT,0777);
printf("the file id:%d\n",file_fd);
struct stat file_buf;
stat(file_path,&file_buf);
int start_pos = file_buf.st_size;
printf("there are have %d\n",start_pos);
char buffer_length[20];
sprintf(buffer_length,"%d",start_pos);
write(new_fd,buffer_length,20);
printf("The upload file is save in:%s\n",file_path);
lseek(file_fd,start_pos,SEEK_SET);
while(1)
{
// printf("hava start transfer\n");
nbytes=read(new_fd,buffer,1024);
// printf("nbytes:%d\n",nbytes);
if(nbytes<=0)
{
break;
}
// printf("nbytes :%d\n",nbytes);
// printf("buffer:%s\n",buffer);
write(file_fd,buffer,nbytes);
bzero(buffer,1024);
}
close(file_fd);
printf("upload successfully");
close(new_fd);
close(sockfd);
}
//download function
void download(int sockfd,int new_fd,char file_path[100],char file_name[100])
{
int nread=0;
int nbytes=0;
char buffer[1024];
int file_fd;
nread=read(new_fd,buffer,1024);
strcpy(file_name,buffer);
printf("file_name:%s\n",file_name);
bzero(buffer,1024);
strcat(file_path,file_name);
file_fd=open(file_path, O_RDWR,0777);
printf("the file id:%d\n",file_fd);
// printf("The download file is save in:%s\n",file_path);
char buffer_length[20];
bzero(buffer_length,20);
read(new_fd,buffer_length,20);
int length = atoi(buffer_length);
printf("the length is :%d\n",length);
lseek(file_fd,length,SEEK_SET);
while(1)
{
nbytes=read(file_fd,buffer,1024);
if(nbytes<=0)
{
break;
}
// printf("nbytes :%d\n",nbytes);
// printf("buffer:%s\n",buffer);
write(new_fd,buffer,nbytes);
bzero(buffer,1024);
}
close(file_fd);
close(new_fd);
close(sockfd);
exit(0);
}
void send_dir(int new_fd, char file_path[100])
{
int cmd_nu=0;
int buffer[100];
DIR *dir;
struct dirent *dirptr;
cmd_nu = read(new_fd,buffer,100);
printf("we get the cmd:%s\n",buffer);
char cmd[100] = "see";
if(!bcmp(buffer,cmd,strlen(cmd)))
{
printf("here\n");
dir=opendir(file_path);
char filedir[1024];
char temp[256];
bzero(temp,256);
bzero(filedir,1024);
while((dirptr=readdir(dir))!=NULL)
{
strcpy(temp,dirptr->d_name);
strcat(temp,"\t");
strcat(filedir,temp);
}
printf("the total file on the server are:%s\n",filedir);
write(new_fd,filedir,strlen(filedir));
closedir(dir);
}
}
int main()
{
int sockfd, new_fd;
char buffer[1024];
char file_path[100]="/home/cuiwei/Desktop/he/";
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
int cmd_read;
char file_name[100];
char cmd_u_d[100];
tcp_init(&sockfd,&new_fd,server_addr,client_addr);
cmd_read=read(new_fd,cmd_u_d,100);
if(!bcmp("u",cmd_u_d,strlen(cmd_u_d)))
{
printf("ok\n");
upload(sockfd,new_fd);
printf("yes\n");
}
else if(!bcmp("d",cmd_u_d,strlen(cmd_u_d))){
send_dir(new_fd,file_path);
download(sockfd,new_fd,file_path,file_name);
}
}
文件传输,支持断点续传,Linux下的
4星 · 超过85%的资源 需积分: 10 132 浏览量
2011-03-22
16:58:56
上传
评论 6
收藏 25KB RAR 举报
ttplayer423
- 粉丝: 5
- 资源: 4
- 1
- 2
- 3
- 4
- 5
前往页