Linux下基于TCP的文件传输
服务器:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
#include <netinet/in.h> #include <sys/types.h> #include <sys/socket.h> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h> #define SERVER_PORT 6666 #define LISTEN_QUEUE 20 #define BUFFER_SIZE 1024 int main(int argc,char **argv) { struct sockaddr_in server_addr; bzero(&server_addr,sizeof(server_addr));//全部置零 //设置地址相关的属性 server_addr.sin_family=AF_INET; server_addr.sin_addr.s_addr=htons(INADDR_ANY); server_addr.sin_port=htons(SERVER_PORT); //创建套接字 int server_socket=socket(AF_INET,SOCK_STREAM,0); if(server_socket<0) { printf("socket create error\n"); exit(1); } //绑定端口 if(bind(server_socket,(struct sockaddr*)&server_addr,sizeof(server_addr))) { printf("bind error\n"); exit(1); } //服务器端监听 if(listen(server_socket,LISTEN_QUEUE)) { printf("Server listen error\n"); exit(1); } //服务器端一直运行 while(1) { pid_t pid; struct sockaddr_in client_addr; socklen_t length=sizeof(client_addr); //accept返回一个新的套接字与客户端进行通信 int new_server_socket=accept(server_socket,(struct sockaddr*)&client_addr,&length); //1*begin****************************************************************************** if(new_server_socket==-1) { printf("accept error\n"); continue; } else { printf("客户端%s连接成功\n",inet_ntoa(client_addr.sin_addr)); pid=fork(); //3*begin**运行子进程************************************************************* if(pid==0) { int data_len,flag=0; char buffer[BUFFER_SIZE]; // 定义文件流 FILE *stream; bzero(buffer,BUFFER_SIZE); strcpy(buffer,"请输入要传输的文件的完整路径:"); strcat(buffer,"\n"); send(new_server_socket,buffer,BUFFER_SIZE,0); bzero(buffer,BUFFER_SIZE); //2*begin**服务器接受数据********************************************* if((stream=fopen("data","w"))==NULL) { printf("file open error\n"); exit(1); } else { bzero(buffer,BUFFER_SIZE); } // printf("正在接收来自%s的文件....\n",inet_ntoa(client_addr.sin_addr)); //先将数据接受到缓冲区buffer中,再写入到新建的文件中 while(data_len=recv(new_server_socket,buffer,BUFFER_SIZE,0)) { flag++; if(flag==1) { printf("正在接收来自%s的文件....\n",inet_ntoa(client_addr.sin_addr)); } if(data_len<0) { printf("接收错误\n"); exit(1); } //向文件中写入数据 int write_len=fwrite(buffer,sizeof(char),data_len,stream); if(write_len>data_len) { printf("file write failed\n"); exit(1); } bzero(buffer,BUFFER_SIZE); } if(flag>0) printf("%s的文件传送完毕\n",inet_ntoa(client_addr.sin_addr)); if(flag==0) printf("%s的文件传输失败\n",inet_ntoa(client_addr.sin_addr)); //2*end**服务器接受数据**************************************************** // rename("data",inet_ntoa(client_addr.sin_addr)); fclose(stream); rename("data",inet_ntoa(client_addr.sin_addr)); exit(1); } //3*end**运行子进程********************************************************** else { close(new_server_socket); } } //1*end************************************************************************************** close(new_server_socket); } return 0; } |
客户端:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
#include <netinet/in.h> #include <sys/types.h> #include <sys/socket.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <arpa/inet.h> #define SERVER_PORT 6666 #define BUFFER_SIZE 1024 int main(int argc,char **argv) { if(argc!=2) { printf("参数错误,清输入两个参数\n"); exit(1); } FILE *stream; struct sockaddr_in server_addr; bzero(&server_addr,sizeof(server_addr)); //把一段内存区的内容全部设置为0 server_addr.sin_family = AF_INET; //internet协议族 server_addr.sin_addr.s_addr = inet_addr(argv[1]); server_addr.sin_port = htons(SERVER_PORT); int sfd; sfd=socket(AF_INET,SOCK_STREAM,0); if(sfd<0) { printf("socket error\n"); exit(0); } if(connect(sfd,(struct sockaddr*)&server_addr,sizeof(server_addr)) < 0) { printf("Can Not Connect To %s\n",argv[1]); exit(1); } char buffer[BUFFER_SIZE]; bzero(buffer,BUFFER_SIZE); int length = recv(sfd,buffer,BUFFER_SIZE,0); if(length < 0) { printf("Recieve Data From Server %s Failed!\n", argv[1]); exit(1); } printf("\n%s\n",buffer); bzero(buffer,BUFFER_SIZE); // fgets(buffer,1024,stdin); // char filename[100]; gets(buffer); //打开文件流 if((stream=fopen(buffer,"r"))==NULL) { printf("the file was not opened\n"); exit(1); } bzero(buffer,BUFFER_SIZE); printf("正在传输...\n"); int len=0; //不断读取并发送数据 while((len=fread(buffer,1,1024,stream))>0) { // printf("len=%d\n",len); if(send(sfd,buffer,len,0)<0) { printf("send file error\n"); break; } bzero(buffer,BUFFER_SIZE); } if(fclose(stream)) { printf("file close error\n"); exit(1); } close(sfd); return 0; } |
在服务器端的程序中,当一个客户端连接,就创建一个独立的进程。
发表评论
要发表评论,您必须先登录。