1.传输子系统的设计
a.系统程序框架搭建
客户端:
补充:
fgets()函数用于键盘的读入:fgets(key,n,stdin) 或者 从文件中读入字符串fgets(str,n,fp);
1 #include <stdio.h> 2 #include <sys/types.h> 3 #include <netinet/in.h> 4 #include <sys/socket.h> 5 #include <sys/stat.h> 6 #include <errno.h> 7 #include <unistd.h> 8 #include <fcntl.h> 9 #include <string.h> 10 11 #define port 3333 12 13 int sockclient; 14 struct sockaddr_in sockaddr1; 15 char ipaddr[15]; 16 17 18 int linkS() 19 { 20 if((sockclient=socket(AF_INET,SOCK_STREAM,0))==-1) 21 { 22 perror("socket"); 23 exit(0); 24 } 25 26 memset(&sockaddr1,0,sizeof(sockaddr1)); 27 sockaddr1.sin_family = AF_INET; 28 sockaddr1.sin_addr.s_addr = inet_addr(ipaddr); 29 sockaddr1.sin_port = htons(port); 30 31 if(connect(sockclient,(struct sockaddr* )&sockaddr1,sizeof(sockaddr1))==-1) 32 { 33 perror("connect"); 34 exit(0); 35 } 36 37 return 1; 38 } 39 40 //~~~~~~~~~~~~~~~~~~~~~~~上传文件~~~~~~~~~~~~~~~~~~~~~~~~~ 41 void upload_file(char *filename) 42 { 43 int fd; 44 char buf[1024]; 45 int count=0; 46 int size = strlen(filename); 47 char cmd = 'U'; 48 49 struct stat fstat; 50 51 if((fd=open(filename,O_RDONLY))==-1) 52 { 53 perror("open: "); 54 return; 55 } 56 57 /*发送上传命令*/ 58 write(sockclient,&cmd,1); 59 60 /*发送文件名*/ 61 write(sockclient,(void *)&size,4); 62 write(sockclient,filename,size); 63 64 /*发送文件长度*/ 65 if(stat(filename,&fstat)==-1) 66 return; 67 68 write(sockclient,(void *)&(fstat.st_size),4); 69 70 /*发送文件内容*/ 71 while((count=read(fd,(void *)buf,1024))>0) 72 { 73 write(sockclient,&buf,count); 74 } 75 76 close(fd); 77 78 } 79 //~~~~~~~~~~~~~~~~~~~~~~~下载文件~~~~~~~~~~~~~~~~~~~~~~~~~ 80 81 void download_file(char *filename) 82 { 83 int fd; 84 char buf[1024]; 85 int count=0; 86 int filesize = 0; 87 int tmpsize = 0; 88 int namesize = 0; 89 char cmd = 'D'; 90 91 int size = strlen(filename); 92 93 /*发送下载命令*/ 94 write(sockclient,(void *)&cmd,1); 95 96 /*发送文件名*/ 97 write(sockclient,&size,4); 98 write(sockclient,filename,size); 99 100 /*创建文件*/ 101 if((fd=open(filename,O_RDWR|O_CREAT,0777))<0) 102 { 103 perror("open error: "); 104 } 105 106 /*接收文件长度*/ 107 read(sockclient,&filesize,4); 108 109 while((count=read(sockclient,(void *)buf,1024))>0) 110 { 111 write(fd,&buf,count); 112 tmpsize += count; 113 if(tmpsize==filesize) 114 break; 115 116 } 117 118 close(fd); 119 } 120 121 122 void quit() 123 { 124 char cmd = 'Q'; 125 126 write(sockclient,(void *)&cmd,1); 127 128 system("clear"); 129 130 exit(0); 131 } 132 133 void menu() 134 { 135 char command; 136 char file_u[30]; 137 char file_d[30]; 138 char tmp; 139 char c; 140 141 while(1) 142 { 143 printf(" ------------------------------ 1.Upload Files ------------------------------ "); 144 printf("------------------------------ 2.Download Files ------------------------------ "); 145 printf("------------------------------ 3.Exit ------------------------------------ "); 146 printf("Please input the Client command:"); 147 148 command=getchar(); 149 150 switch(command) 151 { 152 case '1': 153 { 154 printf("Upload File:"); 155 156 while ((c=getchar()) != ' ' && c != EOF); 157 158 fgets(file_u,30,stdin); 159 160 file_u[strlen(file_u)-1]='