码迷,mamicode.com
首页 > 其他好文 > 详细

利用socket传文件

时间:2015-05-04 17:15:57      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:

欢迎批评

也可以看看这里的比较详细http://www.jb51.net/article/53733.htm


服务端
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/socket.h> #include <arpa/inet.h> #include <unistd.h> #define BUFFER_SIZE 1024 #define FILE_NAME_SIZE 512 void error(char *message); int main(int argc,char*argv[]) { int serv_sock,clnt_sock,i,length,read_length,j; struct sockaddr_in serv_addr,clnt_addr; socklen_t clnt_addr_sz; char file_name[FILE_NAME_SIZE],message[BUFFER_SIZE]; char buffer[BUFFER_SIZE]; if(argc!=2) { printf("usage:%s<port>\n",argv[0]); exit(1); } serv_sock=socket(PF_INET,SOCK_STREAM,0); if(serv_sock==-1) error("socket() error"); memset(&serv_addr,0,sizeof(serv_addr)); serv_addr.sin_family=AF_INET; serv_addr.sin_addr.s_addr=htonl(INADDR_ANY); serv_addr.sin_port=htons(atoi(argv[1])); if(bind(serv_sock,(struct sockaddr*)&serv_addr,sizeof(serv_addr))==-1) error("bind() error"); if(listen(serv_sock,3)==-1) error("listen() error"); clnt_addr_sz=sizeof(clnt_addr); for(i=1;i<=3;i++) //while(1) { clnt_sock=accept(serv_sock,(struct sockaddr*)&clnt_addr,&clnt_addr_sz); if(clnt_sock==-1) error("accept() error"); else printf("connected client %d \n`",i); if(read(clnt_sock,buffer,BUFFER_SIZE)==-1) printf("read() error"); strcpy(file_name,buffer); printf("file‘s name %s\n ",file_name); FILE *fp=fopen(file_name,"r"); if(fp==NULL) printf("file %s can not open \n",file_name); else { bzero(buffer,BUFFER_SIZE); while((length=fread(buffer,sizeof(char),BUFFER_SIZE,fp))>0) { if(write(clnt_sock,buffer,length)==-1) { printf("send file %s failed\n",file_name); break; } } fclose(fp); printf("transfer succeed\n"); } close(clnt_sock); } close(serv_sock); return 0; } void error(char* message) { fputs(message,stderr); fputc(\n,stderr); exit(1); }

 


客户端

1
#include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <unistd.h> 5 #include <arpa/inet.h> 6 #include <sys/socket.h> 7 #define FILE_NAME_SZ 512 8 #define BUFFER_SIZE 1024 9 void error(char* message); 10 11 int main(int argc,char*argv[]) 12 { 13 int sock,temp=0; 14 struct sockaddr_in clnt; 15 int length,write_length; 16 char buffer[BUFFER_SIZE+1]; 17 char filename[FILE_NAME_SZ+1]; 18 char message[BUFFER_SIZE]; 19 sock=socket(PF_INET,SOCK_STREAM,0); 20 if(argc!=3) 21 { 22 printf("usage:%s<ip><port>\n",argv[0]); 23 } 24 if(sock==-1) 25 error("socket() error "); 26 27 memset(&clnt,0,sizeof(clnt)); 28 clnt.sin_family=AF_INET; 29 clnt.sin_addr.s_addr=inet_addr(argv[1]); 30 clnt.sin_port=htons(atoi(argv[2])); 31 32 if(connect(sock,(struct sockaddr*)&clnt,sizeof(clnt))==-1) 33 error("connect() error"); 34 else 35 puts("connect........"); 36 37 printf("input file name\t"); 38 scanf("%s",filename); 39 40 strcpy(buffer,filename); 41 write(sock,buffer,BUFFER_SIZE); 42 43 FILE *fp=fopen(filename,"w"); 44 if(fp==NULL) 45 { 46 printf("open file %s error\n",filename); 47 exit(1); 48 } 49 else 50 { 51 bzero(buffer,BUFFER_SIZE); 52 while((length=read(sock,buffer,BUFFER_SIZE))>0) 53 { 54 if(fwrite(buffer,sizeof(char),length,fp)<length) 55 { 56 printf("file %s write error\n",filename); 57 break; 58 } 59 //printf("receive %s succeed\n",filename); 60 temp=1; 61 } 62 if(temp==1) printf("receive %s succedd\n",filename); 63 else printf("receive %s failed\n",filename); 64 } 65 fclose(fp); 66 close(sock); 67 return 0; 68 } 69 70 void error(char* message) 71 { 72 fputs(message,stderr); 73 fputc(\n,stderr); 74 exit(1); 75 }

 

利用socket传文件

标签:

原文地址:http://www.cnblogs.com/boyiliushui/p/4476334.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!