-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 3666bbe
Showing
15 changed files
with
663 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
SOURCES:=$(wildcard *.c) | ||
OBJS:=$(patsubst %.c,%.o,$(SOURCES)) | ||
ELF:=client | ||
CC:=gcc | ||
CFLAGS:=-Wall | ||
$(ELF):$(OBJS) | ||
gcc $^ -o $@ | ||
clean: | ||
rm -rf $(OBJS) $(ELF) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#include "func.h" | ||
|
||
int main(int argc,char** argv) | ||
{ | ||
check_args(argc,3); | ||
int sfd; | ||
sfd=socket(AF_INET,SOCK_STREAM,0); | ||
check_error(-1,sfd,"socket"); | ||
struct sockaddr_in ser; | ||
bzero(&ser,sizeof(ser)); | ||
ser.sin_family=AF_INET; | ||
ser.sin_port=htons(atoi(argv[2])); //端口号转换为网络字节序 | ||
ser.sin_addr.s_addr=inet_addr(argv[1]); | ||
int ret; | ||
ret=connect(sfd,(struct sockaddr*)&ser,sizeof(struct sockaddr)); | ||
check_error(-1,ret,"connect"); | ||
int len; | ||
char buf[1000]={0}; | ||
//接文件名 | ||
recv_n(sfd,(char*)&len,4); | ||
recv_n(sfd,buf,len); | ||
|
||
//接文件大小:用于打印百分比 | ||
off_t file_size; | ||
double down_load_size=0; | ||
recv_n(sfd,(char*)&len,4); | ||
recv_n(sfd,(char*)&file_size,len); | ||
int fd=open(buf,O_RDWR|O_CREAT,0666); | ||
check_error(-1,fd,"open"); | ||
|
||
//接文件内容,并按大小打印下载百分比 | ||
off_t compare_size=file_size/100; | ||
while(1) | ||
{ | ||
ret=recv_n(sfd,(char*)&len,4); //服务器端若断开了,recv_n返回-1 | ||
if(ret!=-1&&len>0) //recv_n返回-1,代表服务器断开了 | ||
{ | ||
ret=recv_n(sfd,buf,len); | ||
if(-1==ret) | ||
{ | ||
printf("down percent %5.2f%s\n",down_load_size/file_size*100,"%"); | ||
break; | ||
} | ||
write(fd,buf,len); | ||
down_load_size=down_load_size+len; | ||
if(down_load_size>compare_size) | ||
{ | ||
printf("down percent %5.2f%s\r",down_load_size/file_size*100,"%"); | ||
fflush(stdout); | ||
compare_size=compare_size+file_size/100; | ||
} | ||
}else{ | ||
printf("down percent %5.2f%s\n",down_load_size/file_size*100,"%"); | ||
break; | ||
} | ||
} | ||
close(fd); | ||
close(sfd); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#include <sys/uio.h> | ||
#include <sys/epoll.h> | ||
#include <sys/socket.h> | ||
#include <netdb.h> | ||
#include <arpa/inet.h> | ||
#include <netinet/in.h> | ||
#include <sys/time.h> | ||
#include <pthread.h> | ||
#include <signal.h> | ||
#include <sys/msg.h> | ||
#include <sys/sem.h> | ||
#include <sys/shm.h> | ||
#include <sys/ipc.h> | ||
#include <syslog.h> | ||
#include <sys/wait.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <sys/stat.h> | ||
#include <sys/types.h> | ||
#include <unistd.h> | ||
#include <dirent.h> | ||
#include <string.h> | ||
#include <strings.h> | ||
#include <time.h> | ||
#include <grp.h> | ||
#include <pwd.h> | ||
#include <fcntl.h> | ||
#define check_args(a,b) {if(a!=b) {printf("error args\n");\ | ||
return -1;}} | ||
#define check_error(ret_val,ret,func_name) {if(ret_val==ret){\ | ||
perror(func_name);return -1;}} | ||
#define check_thread_error(ret,func_name) {if(0!=ret){\ | ||
printf("%s is failed %d\n",func_name,ret);return -1;}} | ||
typedef struct{ | ||
pid_t pid; | ||
int fds;//子进程的管道对端 | ||
short busy_flag;//忙碌标志,0代表非忙碌,1代码忙碌 | ||
}Data_t,*pData_t; | ||
|
||
int make_child(pData_t,int); | ||
void child_handle(int); | ||
int recv_fd(int,int*); | ||
int send_fd(int,int); | ||
int recv_n(int,char*,int); | ||
#define FILENAME "file" | ||
typedef struct{ | ||
int len; | ||
char buf[1000]; | ||
}train; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#include "func.h" | ||
//循环发送 | ||
int send_n(int sfd,char* p,int len) | ||
{ | ||
int total=0; | ||
int ret; | ||
while(total<len) | ||
{ | ||
ret=send(sfd,p+total,len-total,0); | ||
if(-1==ret) //代表客户端断开:send第一次返回-1 | ||
{ | ||
printf("client is close\n"); | ||
return -1; | ||
} | ||
total=total+ret; | ||
} | ||
return 0; | ||
} | ||
//循环接收 | ||
int recv_n(int sfd,char* p,int len) | ||
{ | ||
int total=0; | ||
int ret; | ||
while(total<len) | ||
{ | ||
ret=recv(sfd,p+total,len-total,0); | ||
if(0==ret) // 代表服务器端断开, recv返回0 | ||
{ | ||
return -1; | ||
} | ||
total=total+ret; | ||
} | ||
return 0; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
SOURCES:=$(wildcard *.c) | ||
OBJS:=$(patsubst %.c,%.o,$(SOURCES)) | ||
ELF:=client | ||
CC:=gcc | ||
CFLAGS:=-Wall | ||
$(ELF):$(OBJS) | ||
gcc $^ -o $@ | ||
clean: | ||
rm -rf $(OBJS) $(ELF) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#include"func.h" | ||
|
||
int main(int argc,char *argv[]) | ||
{ | ||
check_args(argc,3); | ||
int sfd; | ||
struct sockaddr_in ser; | ||
bzero(&ser,sizeof(ser)); | ||
ser.sin_family=AF_INET; | ||
ser.sin_addr.s_addr=inet_addr(argv[1]); | ||
ser.sin_port=htons(atoi(argv[2])); | ||
sfd=socket(AF_INET,SOCK_STREAM,0); | ||
connect(sfd,(struct sockaddr*)&ser,sizeof(ser)); | ||
int len; | ||
char buf[1000]={0}; | ||
|
||
//接文件名 | ||
recv_n(sfd,(char*)&len,4); | ||
recv_n(sfd,buf,len); | ||
|
||
//接文件大小 | ||
int fd=open(buf,O_RDWR|O_CREAT,0666); | ||
check_error(-1,fd,"open"); | ||
off_t file_size; | ||
double download_size=0; | ||
recv_n(sfd,(char*)&len,4); | ||
recv_n(sfd,(char*)&file_size,len); | ||
|
||
//接文件内容:按秒打印下载的百分比 | ||
time_t start,end; | ||
int ret; | ||
start=time(NULL); | ||
while(1) | ||
{ | ||
bzero(buf,sizeof(buf)); | ||
ret=recv_n(sfd,(char*)&len,4); | ||
if(ret!=-1&&len>0) | ||
{ | ||
ret=recv_n(sfd,buf,len); | ||
if(ret==-1) | ||
{ | ||
printf("download %5.2lf%s\n",download_size/file_size*100,"%"); | ||
break; | ||
} | ||
write(fd,buf,len); | ||
download_size=download_size+len; | ||
end=time(NULL); | ||
if(end-start>=1) | ||
{ | ||
printf("download %5.2lf%s\r",download_size/file_size*100,"%"); | ||
fflush(stdout); | ||
start=end; | ||
} | ||
} | ||
else //len等于0时,即读到了文件结束标志 | ||
{ | ||
printf("download success 100%s\n","%"); | ||
break; | ||
} | ||
} | ||
close(fd); | ||
close(sfd); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#include <sys/uio.h> | ||
#include <sys/epoll.h> | ||
#include <sys/socket.h> | ||
#include <netdb.h> | ||
#include <arpa/inet.h> | ||
#include <netinet/in.h> | ||
#include <sys/time.h> | ||
#include <pthread.h> | ||
#include <signal.h> | ||
#include <sys/msg.h> | ||
#include <sys/sem.h> | ||
#include <sys/shm.h> | ||
#include <sys/ipc.h> | ||
#include <syslog.h> | ||
#include <sys/wait.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <sys/stat.h> | ||
#include <sys/types.h> | ||
#include <unistd.h> | ||
#include <dirent.h> | ||
#include <string.h> | ||
#include <strings.h> | ||
#include <time.h> | ||
#include <grp.h> | ||
#include <pwd.h> | ||
#include <fcntl.h> | ||
#define check_args(a,b) {if(a!=b) {printf("error args\n");\ | ||
return -1;}} | ||
#define check_error(ret_val,ret,func_name) {if(ret_val==ret){\ | ||
perror(func_name);return -1;}} | ||
#define check_thread_error(ret,func_name) {if(0!=ret){\ | ||
printf("%s is failed %d\n",func_name,ret);return -1;}} | ||
typedef struct{ | ||
pid_t pid; | ||
int fds;//子进程的管道对端 | ||
short busy_flag;//忙碌标志,0代表非忙碌,1代码忙碌 | ||
}Data_t,*pData_t; | ||
|
||
int make_child(pData_t,int); | ||
void child_handle(int); | ||
int recv_fd(int,int*); | ||
int send_fd(int,int); | ||
int recv_n(int,char*,int); | ||
#define FILENAME "file" | ||
typedef struct{ | ||
int len; | ||
char buf[1000]; | ||
}train; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#include "func.h" | ||
//循环发送 | ||
int send_n(int sfd,char* p,int len) | ||
{ | ||
int total=0; | ||
int ret; | ||
while(total<len) | ||
{ | ||
ret=send(sfd,p+total,len-total,0); | ||
if(-1==ret) //代表客户端断开:send第一次返回-1 | ||
{ | ||
printf("client is close\n"); | ||
return -1; | ||
} | ||
total=total+ret; | ||
} | ||
return 0; | ||
} | ||
//循环接收 | ||
int recv_n(int sfd,char* p,int len) | ||
{ | ||
int total=0; | ||
int ret; | ||
while(total<len) | ||
{ | ||
ret=recv(sfd,p+total,len-total,0); | ||
if(0==ret) // 代表服务器端断开, recv返回0 | ||
{ | ||
return -1; | ||
} | ||
total=total+ret; | ||
} | ||
return 0; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
SOURCES:=$(wildcard *.c) | ||
OBJS:=$(patsubst %.c,%.o,$(SOURCES)) | ||
ELF:=server | ||
CC:=gcc | ||
CFLAGS:=-Wall | ||
$(ELF):$(OBJS) | ||
gcc $^ -o $@ | ||
clean: | ||
rm -rf $(OBJS) $(ELF) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include "func.h" | ||
int make_child(pData_t p,int process_num) | ||
{ | ||
int i; | ||
int fds[2]; | ||
pid_t pid; | ||
for(i=0;i<process_num;i++) | ||
{ | ||
socketpair(AF_LOCAL,SOCK_STREAM,0,fds); //每个子进程均与父进程之间创建一个特殊管道(可读可写)来传递信息 | ||
pid=fork(); | ||
if(0==pid) | ||
{ | ||
close(fds[1]); | ||
child_handle(fds[0]); //子进程必须在child_handle内进行exit退出 | ||
} | ||
close(fds[0]); | ||
p[i].pid=pid; //记录子进程的pid | ||
p[i].fds=fds[1]; //记录子进程的特殊管道的对端 | ||
p[i].busy_flag=0; //忙碌标志,0代表非忙碌,1代表忙碌 | ||
} | ||
return 0; | ||
} | ||
void child_handle(int fds) | ||
{ | ||
int new_fd; | ||
int exit_flag=0; | ||
char notbusy_flag=1; | ||
while(1) | ||
{ | ||
recv_fd(fds,&new_fd,&exit_flag); //等待接收任务 | ||
if(-1==exit_flag) //服务器的退出机制 | ||
{ | ||
exit(0); //子进程退出 | ||
} | ||
tran_file(new_fd); | ||
write(fds,¬busy_flag,1); //写管道通知父进程我不忙了 | ||
} | ||
} | ||
|
||
|
Oops, something went wrong.